Search in sources :

Example 6 with HttpRequestLine

use of org.webpieces.httpparser.api.dto.HttpRequestLine in project webpieces by deanhiller.

the class Http2Translations method translateRequest.

public static HttpRequest translateRequest(Http2Request headers) {
    HttpRequestLine requestLine = new HttpRequestLine();
    HttpRequest req = new HttpRequest();
    req.setRequestLine(requestLine);
    for (Http2Header header : headers.getHeaders()) {
        insertInfo(req, header);
    }
    Http2HeaderStruct headerMap = headers.getHeaderLookupStruct();
    Http2Header method = headerMap.getHeader(Http2HeaderName.METHOD);
    if (method == null)
        throw new IllegalArgumentException(Http2HeaderName.METHOD.name() + "is a required header to translate to http1");
    req.getRequestLine().setMethod(new HttpRequestMethod(method.getValue()));
    Http2Header host = headerMap.getHeader(Http2HeaderName.AUTHORITY);
    if (host == null)
        throw new IllegalArgumentException(Http2HeaderName.AUTHORITY.name() + "is a required header to translate to http1");
    Http2Header path = headerMap.getHeader(Http2HeaderName.PATH);
    if (path == null)
        throw new IllegalArgumentException(Http2HeaderName.PATH.name() + "is a required header to translate to http1");
    HttpUri httpUri = new HttpUri(path.getValue());
    req.getRequestLine().setUri(httpUri);
    return req;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestMethod(org.webpieces.httpparser.api.dto.HttpRequestMethod) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) Http2HeaderStruct(com.webpieces.hpack.api.dto.Http2HeaderStruct)

Example 7 with HttpRequestLine

use of org.webpieces.httpparser.api.dto.HttpRequestLine 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 8 with HttpRequestLine

use of org.webpieces.httpparser.api.dto.HttpRequestLine 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 9 with HttpRequestLine

use of org.webpieces.httpparser.api.dto.HttpRequestLine in project webpieces by deanhiller.

the class TestRequestParsing method createPostRequest.

static HttpRequest createPostRequest() {
    Header header1 = new Header();
    header1.setName(KnownHeaderName.ACCEPT);
    header1.setValue("CooolValue");
    Header header2 = new Header();
    //let's keep the case even though name is case-insensitive..
    header2.setName("CustomerHEADER");
    header2.setValue("betterValue");
    HttpRequestLine requestLine = new HttpRequestLine();
    requestLine.setMethod(KnownHttpMethod.POST);
    requestLine.setUri(new HttpUri("http://myhost.com"));
    HttpRequest request = new HttpRequest();
    request.setRequestLine(requestLine);
    request.addHeader(header1);
    request.addHeader(header2);
    return request;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Header(org.webpieces.httpparser.api.common.Header) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) HttpUri(org.webpieces.httpparser.api.dto.HttpUri)

Example 10 with HttpRequestLine

use of org.webpieces.httpparser.api.dto.HttpRequestLine in project webpieces by deanhiller.

the class TestRequestParsing method testBasic.

@Test
public void testBasic() {
    HttpRequestLine requestLine = new HttpRequestLine();
    requestLine.setMethod(KnownHttpMethod.POST);
    requestLine.setUri(new HttpUri("http://myhost.com"));
    HttpRequest request = new HttpRequest();
    request.setRequestLine(requestLine);
    String result1 = request.toString();
    String result2 = parser.marshalToString(request);
    String msg = "POST http://myhost.com HTTP/1.1\r\n\r\n";
    Assert.assertEquals(msg, result1);
    Assert.assertEquals(msg, result2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) Test(org.junit.Test)

Aggregations

HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)13 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)12 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)12 Header (org.webpieces.httpparser.api.common.Header)10 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)4 DataWrapper (org.webpieces.data.api.DataWrapper)3 HttpData (org.webpieces.httpparser.api.dto.HttpData)3 HttpRequestMethod (org.webpieces.httpparser.api.dto.HttpRequestMethod)2 Http2HeaderStruct (com.webpieces.hpack.api.dto.Http2HeaderStruct)1 Http2Request (com.webpieces.hpack.api.dto.Http2Request)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Executor (java.util.concurrent.Executor)1 Executors (java.util.concurrent.Executors)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)1 BufferPool (org.webpieces.data.api.BufferPool)1