use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Requests method createPostRequest.
public static List<HttpPayload> createPostRequest(String url, String... argTuples) {
if (argTuples.length % 2 != 0)
throw new IllegalArgumentException("argTuples.length must be of even size (key/value)");
HttpUri httpUri = new HttpUri(url);
HttpRequestLine requestLine = new HttpRequestLine();
requestLine.setMethod(KnownHttpMethod.POST);
requestLine.setUri(httpUri);
HttpRequest req = new HttpRequest();
req.setRequestLine(requestLine);
req.addHeader(new Header(KnownHeaderName.HOST, "myhost.com"));
String encodedParams = "";
for (int i = 0; i < argTuples.length; i += 2) {
String key = URLEncoder.encode(argTuples[i], StandardCharsets.UTF_8);
String value = URLEncoder.encode(argTuples[i + 1], StandardCharsets.UTF_8);
if (!"".equals(encodedParams))
encodedParams += "&";
encodedParams += key + "=" + value;
}
HttpData data = new HttpData();
data.setEndOfData(true);
byte[] bytes = encodedParams.getBytes(StandardCharsets.UTF_8);
DataWrapper body = gen.wrapByteArray(bytes);
data.setBody(body);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + body.getReadableSize()));
req.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded"));
List<HttpPayload> payloads = new ArrayList<>();
payloads.add(req);
payloads.add(data);
return payloads;
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class IntegGoogleHttps method start.
public void start() throws InterruptedException {
log.info("starting test to download / page from google");
boolean isHttp = false;
String host = "www.google.com";
int port = 443;
if (isHttp)
port = 80;
HttpRequestLine requestLine = new HttpRequestLine();
requestLine.setMethod(KnownHttpMethod.GET);
requestLine.setUri(new HttpUri("/"));
HttpRequest req = new HttpRequest();
req.setRequestLine(requestLine);
req.addHeader(new Header(KnownHeaderName.HOST, host));
req.addHeader(new Header(KnownHeaderName.ACCEPT, "*/*"));
req.addHeader(new Header(KnownHeaderName.USER_AGENT, "webpieces/0.9"));
HttpSocket socket = createSocket(isHttp, host, port);
socket.connect(new InetSocketAddress(host, port)).thenAccept(p -> sendRequest(socket, req)).exceptionally(e -> reportException(socket, e));
Thread.sleep(100000);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class HttpRequest method getServerToConnectTo.
/**
* Return the SocketAddress from the information in the http request. Sometimes there is not
* enough information in which case one must provide the port number. The host comes from
* the absolute uri first and if host is not there, it comes from the HOST header next and if
* not found there, this method will throw an exception. port parameter is only required
* when there is not an absolute uri. (ie. only HOST header exists)
*
* @param port
* @return
*/
public InetSocketAddress getServerToConnectTo(Integer port) {
UrlInfo urlInfo = getRequestLine().getUri().getUriBreakdown();
String host = urlInfo.getHost();
if (host == null) {
Header hostHeader = getHeaderLookupStruct().getLastInstanceOfHeader(KnownHeaderName.HOST);
if (hostHeader == null)
throw new IllegalStateException("There is no host in url nor in HOST header to be found in this request");
host = hostHeader.getValue();
}
Integer resolvedPort = urlInfo.getResolvedPort();
if (resolvedPort == null) {
if (port == null)
throw new IllegalArgumentException("The port is required since there is no port information in the HttpRequest");
resolvedPort = port;
}
return new InetSocketAddress(host, resolvedPort);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestChunkedParsing method testBasic.
@Test
public void testBasic() {
String chunkedData = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n";
HttpResponse resp = TestResponseParsing.createOkResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
byte[] chunked = chunkedData.getBytes();
byte[] all = new byte[bytes.length + chunked.length];
System.arraycopy(bytes, 0, all, 0, bytes.length);
System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
DataWrapper wrapper = dataGen.wrapByteArray(all);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrapper);
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(5, msgs.size());
HttpPayload msg = msgs.get(0).getHttpResponse();
Assert.assertEquals(resp, msg);
HttpChunk chunk1 = msgs.get(1).getHttpChunk();
String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals("Wiki", first);
HttpChunk chunk3 = msgs.get(3).getHttpChunk();
String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals(" in\r\n\r\nchunks.", third);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestChunkedParsing method testResponseAfterChunked.
@Test
public void testResponseAfterChunked() {
String chunkedData = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n";
HttpResponse resp = TestResponseParsing.createOkResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
byte[] chunked = chunkedData.getBytes();
HttpResponse resp400 = create400Response();
byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
byte[] all = new byte[bytes.length + chunked.length + tail.length];
System.arraycopy(bytes, 0, all, 0, bytes.length);
System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
System.arraycopy(tail, 0, all, bytes.length + chunked.length, tail.length);
DataWrapper wrapper = dataGen.wrapByteArray(all);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrapper);
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(6, msgs.size());
HttpPayload msg = msgs.get(0).getHttpResponse();
Assert.assertEquals(resp, msg);
HttpChunk chunk1 = msgs.get(1).getHttpChunk();
String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals("Wiki", first);
HttpChunk chunk3 = msgs.get(3).getHttpChunk();
String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals(" in\r\n\r\nchunks.", third);
HttpPayload tailMsg = msgs.get(5);
Assert.assertEquals(resp400, tailMsg);
}
Aggregations