Example #1
0
  private static Request transformRequest(HttpRequest request, Object tag) {
    Request.Builder builder = new Request.Builder();

    RequestLine requestLine = request.getRequestLine();
    String method = requestLine.getMethod();
    builder.url(requestLine.getUri());

    String contentType = null;
    for (Header header : request.getAllHeaders()) {
      String name = header.getName();
      if ("Content-Type".equals(name)) {
        contentType = header.getValue();
      } else {
        builder.header(name, header.getValue());
      }
    }

    RequestBody body = null;
    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
      if (entity != null) {
        // Wrap the entity in a custom Body which takes care of the content, length, and type.
        body = new HttpEntityBody(entity, contentType);

        Header encoding = entity.getContentEncoding();
        if (encoding != null) {
          builder.header(encoding.getName(), encoding.getValue());
        }
      }
    }
    builder.method(method, body);
    builder.tag(tag);

    return builder.build();
  }
 public HttpRequest newHttpRequest(final RequestLine requestline)
     throws MethodNotSupportedException {
   if (requestline == null) {
     throw new IllegalArgumentException("Request line may not be null");
   }
   String method = requestline.getMethod();
   String uri = requestline.getUri();
   return newHttpRequest(method, uri);
 }
 public BasicHttpRequest(final RequestLine requestline) {
   super();
   if (requestline == null) {
     throw new IllegalArgumentException("Request line may not be null");
   }
   this.requestline = requestline;
   this.method = requestline.getMethod();
   this.uri = requestline.getUri();
 }
  @Override
  public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
      throws ProtocolException {
    RequestLine line = request.getRequestLine();

    String location = getLocation(request, response, context).toString();
    Log.i(
        TAG,
        "Following redirection: " + line.getMethod() + " " + line.getUri() + " -> " + location);

    return RequestBuilder.copy(request)
        .setUri(location)
        .removeHeaders(
            "Content-Length") // Content-Length will be set again automatically, if required;
        // remove it now to avoid duplicate header
        .build();
  }
  @Test
  public void testBasicMessageParsing() throws Exception {
    final String s =
        "GET / HTTP/1.1\r\n"
            + "Host: localhost\r\n"
            + "User-Agent: whatever\r\n"
            + "Cookie: c1=stuff\r\n"
            + "\r\n";
    final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);

    final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(inbuffer);
    final HttpRequest httprequest = parser.parse();

    final RequestLine reqline = httprequest.getRequestLine();
    Assert.assertNotNull(reqline);
    Assert.assertEquals("GET", reqline.getMethod());
    Assert.assertEquals("/", reqline.getUri());
    Assert.assertEquals(HttpVersion.HTTP_1_1, reqline.getProtocolVersion());
    final Header[] headers = httprequest.getAllHeaders();
    Assert.assertEquals(3, headers.length);
  }
  @Test
  public void testMessageParsingTimeout() throws Exception {
    final String s =
        "GET \000/ HTTP/1.1\r\000\n"
            + "Host: loca\000lhost\r\n"
            + "User-Agent: whatever\r\n"
            + "Coo\000kie: c1=stuff\r\n"
            + "\000\r\n";
    final SessionInputBuffer inbuffer =
        new SessionInputBufferMock(new TimeoutByteArrayInputStream(s.getBytes(Consts.ASCII)), 16);

    final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(inbuffer);

    int timeoutCount = 0;

    HttpRequest httprequest = null;
    for (int i = 0; i < 10; i++) {
      try {
        httprequest = parser.parse();
        break;
      } catch (final InterruptedIOException ex) {
        timeoutCount++;
      }
    }
    Assert.assertNotNull(httprequest);
    Assert.assertEquals(5, timeoutCount);

    @SuppressWarnings("null") // httprequest cannot be null here
    final RequestLine reqline = httprequest.getRequestLine();
    Assert.assertNotNull(reqline);
    Assert.assertEquals("GET", reqline.getMethod());
    Assert.assertEquals("/", reqline.getUri());
    Assert.assertEquals(HttpVersion.HTTP_1_1, reqline.getProtocolVersion());
    final Header[] headers = httprequest.getAllHeaders();
    Assert.assertEquals(3, headers.length);
  }
 /* Assert.asserts that the components of two request lines match. */
 public static boolean equivalent(RequestLine l1, RequestLine l2) {
   return (l1.getMethod().equals(l2.getMethod())
       && l1.getProtocolVersion().equals(l2.getProtocolVersion())
       && l1.getUri().equals(l2.getUri()));
 }