Search in sources :

Example 36 with Header

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;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) ArrayList(java.util.ArrayList) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 37 with Header

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);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) BufferPool(org.webpieces.data.api.BufferPool) LoggerFactory(org.slf4j.LoggerFactory) ChannelManager(org.webpieces.nio.api.ChannelManager) HttpResponseListener(org.webpieces.httpclient.api.HttpResponseListener) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpParserFactory(org.webpieces.httpparser.api.HttpParserFactory) Header(org.webpieces.httpparser.api.common.Header) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) HttpClient(org.webpieces.httpclient.api.HttpClient) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) HttpParser(org.webpieces.httpparser.api.HttpParser) KnownHeaderName(org.webpieces.httpparser.api.common.KnownHeaderName) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) HttpClientFactory(org.webpieces.httpclient.api.HttpClientFactory) HttpSocket(org.webpieces.httpclient.api.HttpSocket) BufferCreationPool(org.webpieces.data.api.BufferCreationPool) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) KnownHttpMethod(org.webpieces.httpparser.api.dto.KnownHttpMethod) HttpSocket(org.webpieces.httpclient.api.HttpSocket) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) InetSocketAddress(java.net.InetSocketAddress) HttpUri(org.webpieces.httpparser.api.dto.HttpUri)

Example 38 with Header

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);
}
Also used : Header(org.webpieces.httpparser.api.common.Header) InetSocketAddress(java.net.InetSocketAddress)

Example 39 with Header

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 40 with Header

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Aggregations

Header (org.webpieces.httpparser.api.common.Header)64 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)32 Test (org.junit.Test)24 DataWrapper (org.webpieces.data.api.DataWrapper)18 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)16 FullResponse (org.webpieces.webserver.test.FullResponse)16 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)13 WebserverForTest (org.webpieces.webserver.WebserverForTest)12 HttpData (org.webpieces.httpparser.api.dto.HttpData)11 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)10 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)10 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)10 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)9 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)9 Http2Response (com.webpieces.hpack.api.dto.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)5 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4