Beispiel #1
0
 static void enhanceEntity(final HttpEntityEnclosingRequest request) {
   final HttpEntity entity = request.getEntity();
   if (entity != null && !entity.isRepeatable() && !isEnhanced(entity)) {
     final HttpEntity proxy =
         (HttpEntity)
             Proxy.newProxyInstance(
                 HttpEntity.class.getClassLoader(),
                 new Class<?>[] {HttpEntity.class},
                 new RequestEntityExecHandler(entity));
     request.setEntity(proxy);
   }
 }
Beispiel #2
0
  @Override
  public org.zalando.logbook.HttpRequest withBody() throws IOException {
    if (request instanceof HttpEntityEnclosingRequest) {
      final HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) this.request;
      this.body = toByteArray(request.getEntity());
      request.setEntity(new ByteArrayEntity(body));
    } else {
      this.body = new byte[0];
    }

    return this;
  }
Beispiel #3
0
    private void zza(HttpEntityEnclosingRequest httpentityenclosingrequest)
    {
        StringBuffer stringbuffer;
        stringbuffer = new StringBuffer();
        org.apache.http.Header aheader[] = httpentityenclosingrequest.getAllHeaders();
        int k = aheader.length;
        for (int i = 0; i < k; i++)
        {
            stringbuffer.append(aheader[i].toString()).append("\n");
        }

        stringbuffer.append(httpentityenclosingrequest.getRequestLine().toString()).append("\n");
        if (httpentityenclosingrequest.getEntity() == null)
        {
            break MISSING_BLOCK_LABEL_147;
        }
        httpentityenclosingrequest = httpentityenclosingrequest.getEntity().getContent();
        if (httpentityenclosingrequest == null)
        {
            break MISSING_BLOCK_LABEL_147;
        }
        int j = httpentityenclosingrequest.available();
        if (j > 0)
        {
            try
            {
                byte abyte0[] = new byte[j];
                httpentityenclosingrequest.read(abyte0);
                stringbuffer.append("POST:\n");
                stringbuffer.append(new String(abyte0)).append("\n");
            }
            // Misplaced declaration of an exception variable
            catch (HttpEntityEnclosingRequest httpentityenclosingrequest)
            {
                zzae.zzac("Error Writing hit to log...");
            }
        }
        zzae.zzaa(stringbuffer.toString());
        return;
    }
Beispiel #4
0
 public String renderRequestText(HttpRequest request) {
   StringBuilder sb = new StringBuilder();
   sb.append(renderRequestStartLine(request));
   sb.append(renderHeaders(request.getAllHeaders()));
   if (request instanceof HttpEntityEnclosingRequest) {
     final HttpEntityEnclosingRequest requestEntity = (HttpEntityEnclosingRequest) request;
     final String body = renderEntityIfAscii(requestEntity.getEntity());
     if (body != null) {
       sb.append("\n");
       sb.append(body);
     }
   }
   return sb.toString();
 }
  /** Generates a cURL command equivalent to the given request. */
  private static String toCurl(HttpUriRequest request) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header : request.getAllHeaders()) {
      builder.append("--header \"");
      builder.append(header.toString().trim());
      builder.append("\" ");
    }

    URI uri = request.getURI();

    // If this is a wrapped request, use the URI from the original
    // request instead. getURI() on the wrapper seems to return a
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
      HttpRequest original = ((RequestWrapper) request).getOriginal();
      if (original instanceof HttpUriRequest) {
        uri = ((HttpUriRequest) original).getURI();
      }
    }

    builder.append("\"");
    builder.append(uri);
    builder.append("\"");

    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
      HttpEntity entity = entityRequest.getEntity();
      if (entity != null && entity.isRepeatable()) {
        if (entity.getContentLength() < 1024) {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          entity.writeTo(stream);
          String entityString = stream.toString();

          // TODO: Check the content type, too.
          builder.append(" --data-ascii \"").append(entityString).append("\"");
        } else {
          builder.append(" [TOO MUCH DATA TO INCLUDE]");
        }
      }
    }

    return builder.toString();
  }
  @Test
  public void shouldProxyTheRequestMethodUriBodyAndContentType()
      throws ServletException, IOException, URISyntaxException {
    CloseableHttpClient mockHttpClient =
        mock(CloseableHttpClient.class, Mockito.RETURNS_DEEP_STUBS);

    AbstractLightblueProxyServlet servlet =
        getTestServlet(mockHttpClient, null, "http://myservice.com", null);

    HttpServletRequest stubRequest =
        new StubHttpServletRequest(
            "http://my.site.com/app/get/the/thing?foo=bar",
            "GET",
            "{test:0}",
            "application/json",
            "/get/*");

    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    ServletOutputStream outputStream = new FakeServletOutputStream(new ByteArrayOutputStream());
    when(mockResponse.getOutputStream()).thenReturn(outputStream);

    servlet.service(stubRequest, mockResponse);

    ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);

    verify(mockHttpClient).execute(requestCaptor.capture());

    HttpUriRequest request = requestCaptor.getValue();

    assertEquals("GET", request.getMethod());
    assertEquals(new URI("http://myservice.com/the/thing?foo=bar"), request.getURI());
    assertThat(request, instanceOf(HttpEntityEnclosingRequest.class));
    assertEquals(1, request.getHeaders("content-type").length);
    assertEquals("application/json", request.getHeaders("content-type")[0].getValue());

    HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) request;
    ByteArrayOutputStream entityOutStream = new ByteArrayOutputStream();
    entityEnclosingRequest.getEntity().writeTo(entityOutStream);

    byte[] expectedEntityBytes = new byte[stubRequest.getContentLength()];
    stubRequest.getInputStream().read(expectedEntityBytes, 0, stubRequest.getContentLength());

    assertEquals(new String(expectedEntityBytes), entityOutStream.toString());
  }
  public void handle(HttpRequest request, HttpResponse response, HttpContext argument)
      throws HttpException, IOException {
    if (BasicAuthHelper.isAuthenticated(request) == false) {
      BasicAuthHelper.unauthedResponse(response);

      return;
    }

    response.setStatusCode(HttpStatus.SC_OK);

    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;

      HttpEntity entity = enclosingRequest.getEntity();

      String entityString = EntityUtils.toString(entity);

      Uri u = Uri.parse("http://localhost/?" + entityString);

      JSONObject arguments = null;

      try {
        arguments = new JSONObject(URLDecoder.decode(u.getQueryParameter("json"), "UTF-8"));
      } catch (JSONException e) {
        LogManager.getInstance(this._context).logException(e);

        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);

        StringEntity body = new StringEntity(e.toString());
        body.setContentType("text/plain");

        response.setEntity(body);

        return;
      }

      if (arguments != null) {
        try {
          JavaScriptEngine engine = new JavaScriptEngine(this._context);

          String action = arguments.getString("action");

          if ("fetch".equals(action)) {
            if (arguments.has("keys")) {
              JSONObject result = new JSONObject();
              result.put("status", "success");

              JSONArray keys = arguments.getJSONArray("keys");

              JSONObject values = new JSONObject();

              for (int i = 0; i < keys.length(); i++) {
                String key = keys.getString(i);

                String value = engine.fetchString(key);

                values.put(key, value);
              }

              result.put("values", values);

              StringEntity body = new StringEntity(result.toString(2));
              body.setContentType("application/json");

              response.setEntity(body);

              return;
            }
          } else if ("put".equals(action)) {
            JSONArray names = arguments.names();

            for (int i = 0; i < names.length(); i++) {
              String name = names.getString(i);

              if ("action".equals(name) == false) {
                String value = arguments.getString(name);

                if (value != null) engine.persistString(name, value);
              }
            }

            JSONObject result = new JSONObject();
            result.put("status", "success");

            StringEntity body = new StringEntity(result.toString(2));
            body.setContentType("application/json");

            response.setEntity(body);

            return;
          }
        } catch (JSONException e) {
          LogManager.getInstance(this._context).logException(e);

          response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);

          StringEntity body = new StringEntity(e.toString());
          body.setContentType("text/plain");

          response.setEntity(body);

          return;
        }
      }
    }

    response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);

    StringEntity body = new StringEntity(this._context.getString(R.string.error_malformed_request));
    body.setContentType("text/plain");

    response.setEntity(body);
  }