/** Parses input stream and creates new <code>HttpRequest</code> object. */ public static HttpRequest readFrom(InputStream in) { BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(in, StringPool.ISO_8859_1)); } catch (UnsupportedEncodingException uneex) { return null; } HttpRequest httpRequest = new HttpRequest(); String line; try { line = reader.readLine(); } catch (IOException ioex) { throw new HttpException(ioex); } if (!StringUtil.isBlank(line)) { String[] s = StringUtil.splitc(line, ' '); httpRequest.method(s[0]); httpRequest.path(s[1]); httpRequest.httpVersion(s[2]); httpRequest.readHeaders(reader); httpRequest.readBody(reader); } return httpRequest; }
@Test public void testFileUpload() throws IOException { HttpRequest request = HttpRequest.get("http://jodd.org/?id=173"); request.header("User-Agent", "Scaly").form("one", "funny"); File tempFile = FileUtil.createTempFile(); tempFile.deleteOnExit(); FileUtil.writeString(tempFile, "qwerty"); request.form("two", tempFile); byte[] bytes = request.toByteArray(); // read HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes)); Map<String, Object[]> httpParams2 = request2.form(); assertEquals(request.method(), request2.method()); assertEquals(request.path(), request2.path()); assertEquals(request.queryString(), request2.queryString()); assertEquals(request.header("User-Agent"), request2.header("User-Agent")); assertEquals(request.header("Content-Type"), request2.header("content-type")); assertEquals(request.header("Content-Length"), request2.header("content-length")); Map params1 = request.form(); Map params2 = request2.form(); assertEquals(params1.size(), params2.size()); assertEquals(params2.get("one"), params2.get("one")); FileUpload fu = (FileUpload) httpParams2.get("two")[0]; assertEquals(6, fu.getSize()); String str = new String(fu.getFileContent()); assertEquals("qwerty", str); tempFile.delete(); }
@Test public void testInOutForm() { HttpRequest request = HttpRequest.get("http://jodd.org/?id=173"); request.header("User-Agent", "Scaly"); request.form("one", "funny"); byte[] bytes = request.toByteArray(); // read HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes)); assertEquals(request.method(), request2.method()); assertEquals(request.path(), request2.path()); assertEquals(request.queryString(), request2.queryString()); assertEquals(request.header("User-Agent"), request2.header("User-Agent")); assertEquals(request.header("Content-Type"), request2.header("content-type")); assertEquals(request.header("Content-Length"), request2.header("content-length")); Map params1 = request.form(); Map params2 = request2.form(); assertEquals(params1.size(), params2.size()); assertEquals(params2.get("one"), params2.get("one")); }
/** * 用于签名的字符串 * * @param bucket Bucket * @param key 文件key * @param contentMd5 内容的md5值 * @param date 日期 * @param request 请求对象 * @return 用于签名的字符串 */ public static String strToSign( String bucket, String key, String contentMd5, String date, HttpRequest request) { String contentType = request.contentType(); if (StrUtil.isBlank(contentType)) { contentType = "text/plain"; request.contentType(contentType); log.warn("Content-Type header is empty, use default Content-Type: {}", contentType); } return StrUtil.builder() .append(request.method()) .append("\n") .append(StrUtil.nullToEmpty(contentMd5)) .append("\n") .append(contentType) .append("\n") .append(StrUtil.nullToEmpty(date)) .append("\n") .append(canonicalizedUcloudHeaders(request)) // canonicalizedUcloudHeaders尾部带一个换行符 .append(canonicalizedResource(bucket, key)) .toString(); }
@Test public void testSet() { HttpRequest httpRequest = new HttpRequest(); httpRequest.set("GET http://jodd.org:173/index.html?light=true"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("jodd.org", httpRequest.host()); assertEquals(173, httpRequest.port()); assertEquals("/index.html", httpRequest.path()); assertEquals("true", httpRequest.query().get("light")[0]); httpRequest = new HttpRequest(); httpRequest.set("http://jodd.org:173/index.html?light=true"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("jodd.org", httpRequest.host()); assertEquals(173, httpRequest.port()); assertEquals("/index.html", httpRequest.path()); assertEquals("true", httpRequest.query().get("light")[0]); httpRequest = new HttpRequest(); httpRequest.set("jodd.org:173/index.html?light=true"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("jodd.org", httpRequest.host()); assertEquals(173, httpRequest.port()); assertEquals("/index.html", httpRequest.path()); assertEquals("true", httpRequest.query().get("light")[0]); httpRequest = new HttpRequest(); httpRequest.set("jodd.org/index.html?light=true"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("jodd.org", httpRequest.host()); assertEquals(80, httpRequest.port()); assertEquals("/index.html", httpRequest.path()); assertEquals("true", httpRequest.query().get("light")[0]); httpRequest = new HttpRequest(); httpRequest.set("/index.html?light=true"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("localhost", httpRequest.host()); assertEquals(80, httpRequest.port()); assertEquals("/index.html", httpRequest.path()); assertEquals("true", httpRequest.query().get("light")[0]); httpRequest = new HttpRequest(); httpRequest.set("http://jodd.org"); assertEquals("GET", httpRequest.method()); assertEquals("http", httpRequest.protocol()); assertEquals("jodd.org", httpRequest.host()); assertEquals(80, httpRequest.port()); assertEquals("/", httpRequest.path()); }