Ejemplo n.º 1
0
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, FullHttpMessage msg)
      throws Exception { // 1
    // Request header 처리
    if (msg instanceof HttpRequest) { // 2
      this.request = (HttpRequest) msg; // 3

      if (HttpHeaders.is100ContinueExpected(request)) {
        send100Continue(ctx);
      }

      HttpHeaders headers = request.headers(); // 4
      if (!headers.isEmpty()) {
        for (Map.Entry<String, String> h : headers) {
          String key = h.getKey();
          if (usingHeader.contains(key)) { // 5
            reqData.put(key, h.getValue()); // 6
          }
        }
      }

      reqData.put("REQUEST_URI", request.getUri()); // 7
      reqData.put("REQUEST_METHOD", request.getMethod().name()); // 8
    }

    if (msg instanceof HttpContent) { // 9
      HttpContent httpContent = (HttpContent) msg; // 10

      ByteBuf content = httpContent.content(); // 11

      if (msg instanceof LastHttpContent) { // 12
        System.out.println("LastHttpContent message received!!" + request.getUri());

        LastHttpContent trailer = (LastHttpContent) msg;

        readPostData(); // 13

        ApiRequest service = ServiceDispatcher.dispatch(reqData); // 14

        try {
          service.executeService(); // 15

          apiResult = service.getApiResult(); // 16
        } finally {
          reqData.clear();
        }

        if (!writeResponse(trailer, ctx)) { // 17
          ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }
        reset();
      }
    }
  }
Ejemplo n.º 2
0
 @Override
 public void channelRead0(final ChannelHandlerContext ctx, HttpRequest req) throws Exception {
   if (req.getUri().equals("/hello")) {
     ctx.executor().schedule(new HelloWorldRunnable(ctx), 10, TimeUnit.SECONDS);
   } else {
     ctx.fireChannelRead(req);
   }
 }
 @Override
 protected void doGet(ChannelHandlerContext ctx, HttpRequest httpReq) {
   getCount++;
   processTime();
   QueryStringDecoder reqDecoder = new QueryStringDecoder(httpReq.getUri());
   String data = reqDecoder.parameters().get("data").get(0);
   Payload payload = JSONSerializer.INSTANCE.fromString(data, Payload.class);
   writeJSON(ctx, httpReq, payload);
   this.writeContent(ctx, httpReq, "do get", "text/plain");
 }
Ejemplo n.º 4
0
  public static ResteasyUriInfo extractUriInfo(
      HttpRequest request, String contextPath, String protocol) {
    String host = HttpHeaders.getHost(request, "unknown");
    String uri = request.getUri();

    String uriString = protocol + "://" + host + uri;
    URI absoluteURI = URI.create(uriString);
    URI noQuery = UriBuilder.fromUri(uriString).replaceQuery(null).build();
    return new ResteasyUriInfo(uriString, absoluteURI.getRawQuery(), contextPath);
  }
Ejemplo n.º 5
0
  /**
   * Get the mover channel for a certain HTTP request. The mover channel is identified by UUID
   * generated upon mover start and sent back to the door as a part of the address info.
   *
   * @param request HttpRequest that was sent by the client
   * @param exclusive True if the mover channel exclusively is to be opened in exclusive mode. False
   *     if the mover channel can be shared with other requests.
   * @return Mover channel for specified UUID
   * @throws IllegalArgumentException Request did not include UUID or no mover channel found for
   *     UUID in the request
   */
  private NettyTransferService<HttpProtocolInfo>.NettyMoverChannel open(
      HttpRequest request, boolean exclusive) throws IllegalArgumentException, URISyntaxException {
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());

    Map<String, List<String>> params = queryStringDecoder.parameters();
    if (!params.containsKey(HttpTransferService.UUID_QUERY_PARAM)) {
      if (!request.getUri().equals("/favicon.ico")) {
        _logger.error(
            "Received request without UUID in the query " + "string. Request-URI was {}",
            request.getUri());
      }

      throw new IllegalArgumentException("Query string does not include any UUID.");
    }

    List<String> uuidList = params.get(HttpTransferService.UUID_QUERY_PARAM);
    if (uuidList.isEmpty()) {
      throw new IllegalArgumentException("UUID parameter does not include any value.");
    }

    UUID uuid = UUID.fromString(uuidList.get(0));
    NettyTransferService<HttpProtocolInfo>.NettyMoverChannel file =
        _server.openFile(uuid, exclusive);
    if (file == null) {
      throw new IllegalArgumentException(
          "Request is no longer valid. " + "Please resubmit to door.");
    }

    URI uri = new URI(request.getUri());
    FsPath requestedFile = FsPath.create(uri.getPath());
    FsPath transferFile = FsPath.create(file.getProtocolInfo().getPath());

    if (!requestedFile.equals(transferFile)) {
      _logger.warn(
          "Received an illegal request for file {}, while serving {}", requestedFile, transferFile);
      throw new IllegalArgumentException(
          "The file you specified does " + "not match the UUID you specified!");
    }

    _files.add(file);

    return file;
  }
Ejemplo n.º 6
0
  @Override
  public Context process(final HttpObject request, final Context context) {
    if (request instanceof HttpRequest) {
      final HttpRequest httpRequest = (HttpRequest) request;
      final QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.getUri());
      context.getQueryParams().putAll(queryStringDecoder.parameters());
      context.getCookies().putAll(readCookie(httpRequest));
    }

    return context;
  }
 /**
  * Returns the route URI for the current request
  *
  * @param request HttpRequest which contains the request URI of the current session
  * @return Returns the route
  * @throws URISyntaxException
  */
 public TrackingType setTrackingType(HttpRequest request) throws URISyntaxException {
   URI uri = new URI(request.getUri());
   if (uri.getPath().startsWith("/trackingPoint")) {
     return TrackingType.TRACKING_POINT;
   } else if (uri.getPath().startsWith("/startTracking")) {
     return TrackingType.START_TRACKING;
   } else if (uri.getPath().startsWith("/stopTracking")) {
     return TrackingType.STOP_TRACKING;
   } else {
     return TrackingType.UNKNOWN;
   }
 }
Ejemplo n.º 8
0
  private static boolean isRequestTo(HttpRequest request, String uri) {
    URI decodedUri;
    try {
      decodedUri = new URI(request.getUri());
    } catch (URISyntaxException e) {
      return false;
    }

    return HttpVersion.HTTP_1_1.equals(request.getProtocolVersion())
        && USER_AGENT.equals(request.getHeader(HttpHeaders.Names.USER_AGENT))
        && HttpMethod.POST.equals(request.getMethod())
        && uri.equals(decodedUri.getPath());
  }
Ejemplo n.º 9
0
    public void messageReceived(ChannelHandlerContext ctx, Object o) throws Exception {

      if (o instanceof HttpRequest) { // DefaultHttpRequest ) {
        queue.add((HttpRequest) o);
      } else if (o instanceof LastHttpContent) {

        HttpRequest req = queue.remove();

        req.getMethod();
        req.getUri();
        req.headers();

        LastHttpContent content = (LastHttpContent) o;

        ByteBuf buf = content.content();

        if (buf.readableBytes() > 0) {
          Gson gson = GsonFactory.createBuilder().create();

          Reader in = new InputStreamReader(new ByteBufInputStream(buf), "utf-8");
          Object v =
              gson.fromJson(in, Class.forName("com.logbook.logbook.resources.logs.LogEntryDTO"));
          System.out.println("v = " + v);
        }

        System.out.println(
            req.getMethod() + " " + req.getUri() + "    -- " + buf.readableBytes() + " bytes");

        HttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        r.headers().add("Content-Length", "0");
        ctx.write(r);

      } else {
        System.out.println("o = " + o + " : " + o.getClass());
      }
    }
  @Override
  public void handleWebRequest(
      ChannelHandlerContext ctx,
      HttpRequest nettyRequest,
      HttpFileUpload upload,
      Set<HttpCookie> cookiesToAdd)
      throws IOException {
    NettyHttpRequest request =
        new NettyHttpRequest(nettyRequest, parentHandler.getWebServer().getLog());
    NettyHttpResponse response = new NettyHttpResponse(ctx, extraHttpContentHeaders);
    response.addCookies(cookiesToAdd);

    DefaultFullHttpResponse res;
    try {
      requestHandler.handle(request, upload, response);

      res =
          new DefaultFullHttpResponse(
              HttpVersion.HTTP_1_1,
              HttpResponseStatus.valueOf(response.getResponseCode()),
              response.getChannelBuffer());

      String contentType = response.getContentType();
      if (contentType != null) {
        addHeader(res, HttpHeaders.Names.CONTENT_TYPE, contentType);
      }

      parentHandler.addHttpResponseHeaders(res, response.getContentHeaders());
      parentHandler.sendHttpResponse(ctx, nettyRequest, res, true, false);

      parentHandler
          .getWebServer()
          .getLog()
          .debug("Dynamic content handler for %s completed", uriPrefix);
    } catch (Throwable e) {
      parentHandler
          .getWebServer()
          .getLog()
          .error(
              String.format(
                  "Error while handling dynamic web server request %s", nettyRequest.getUri()),
              e);

      parentHandler.sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
  }
    @Override
    public boolean preCall(HttpRequest request, HttpResponder responder, HandlerInfo handlerInfo) {
      HTTPMonitoringEvent httpMonitoringEvent = new HTTPMonitoringEvent();
      httpMonitoringEvent.setTimestamp(System.currentTimeMillis());
      httpMonitoringEvent.setStartNanoTime(System.nanoTime());
      if (serviceClass == null) {
        Method method = handlerInfo.getMethod();
        Class<?> serviceClass = method.getDeclaringClass();
        this.serviceClass = serviceClass.getName();
        serviceName = serviceClass.getSimpleName();
        serviceMethod = method.getName();
        if (serviceClass.isAnnotationPresent(Path.class)) {
          Path path = serviceClass.getAnnotation(Path.class);
          servicePath = path.value();
        }
      }
      httpMonitoringEvent.setServiceClass(serviceClass);
      httpMonitoringEvent.setServiceName(serviceName);
      httpMonitoringEvent.setServiceMethod(serviceMethod);
      httpMonitoringEvent.setRequestUri(request.getUri());
      httpMonitoringEvent.setServiceContext(servicePath);

      HttpHeaders httpHeaders = request.headers();

      httpMonitoringEvent.setHttpMethod(request.getMethod().name());
      httpMonitoringEvent.setContentType(httpHeaders.get(HttpHeaders.Names.CONTENT_TYPE));
      String contentLength = httpHeaders.get(HttpHeaders.Names.CONTENT_LENGTH);
      if (contentLength != null) {
        httpMonitoringEvent.setRequestSizeBytes(Long.parseLong(contentLength));
      }
      httpMonitoringEvent.setReferrer(httpHeaders.get(HttpHeaders.Names.REFERER));

      handlerInfo.setAttribute(MONITORING_EVENT, httpMonitoringEvent);

      return true;
    }
Ejemplo n.º 12
0
  private SpdySynStreamFrame createSynStreamFrame(HttpMessage httpMessage) throws Exception {
    boolean chunked = httpMessage.isChunked();

    // Get the Stream-ID, Associated-To-Stream-ID, Priority, URL, and scheme from the headers
    int streamID = SpdyHttpHeaders.getStreamID(httpMessage);
    int associatedToStreamID = SpdyHttpHeaders.getAssociatedToStreamID(httpMessage);
    byte priority = SpdyHttpHeaders.getPriority(httpMessage);
    String URL = SpdyHttpHeaders.getUrl(httpMessage);
    String scheme = SpdyHttpHeaders.getScheme(httpMessage);
    SpdyHttpHeaders.removeStreamID(httpMessage);
    SpdyHttpHeaders.removeAssociatedToStreamID(httpMessage);
    SpdyHttpHeaders.removePriority(httpMessage);
    SpdyHttpHeaders.removeUrl(httpMessage);
    SpdyHttpHeaders.removeScheme(httpMessage);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpMessage.removeHeader(HttpHeaders.Names.CONNECTION);
    httpMessage.removeHeader("Keep-Alive");
    httpMessage.removeHeader("Proxy-Connection");
    httpMessage.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);

    SpdySynStreamFrame spdySynStreamFrame =
        new DefaultSpdySynStreamFrame(streamID, associatedToStreamID, priority);

    // Unfold the first line of the message into name/value pairs
    if (httpMessage instanceof HttpRequest) {
      HttpRequest httpRequest = (HttpRequest) httpMessage;
      SpdyHeaders.setMethod(spdyVersion, spdySynStreamFrame, httpRequest.getMethod());
      SpdyHeaders.setUrl(spdyVersion, spdySynStreamFrame, httpRequest.getUri());
      SpdyHeaders.setVersion(spdyVersion, spdySynStreamFrame, httpMessage.getProtocolVersion());
    }
    if (httpMessage instanceof HttpResponse) {
      HttpResponse httpResponse = (HttpResponse) httpMessage;
      SpdyHeaders.setStatus(spdyVersion, spdySynStreamFrame, httpResponse.getStatus());
      SpdyHeaders.setUrl(spdyVersion, spdySynStreamFrame, URL);
      spdySynStreamFrame.setUnidirectional(true);
    }

    // Replace the HTTP host header with the SPDY host header
    if (spdyVersion >= 3) {
      String host = HttpHeaders.getHost(httpMessage);
      httpMessage.removeHeader(HttpHeaders.Names.HOST);
      SpdyHeaders.setHost(spdySynStreamFrame, host);
    }

    // Set the SPDY scheme header
    if (scheme == null) {
      scheme = "https";
    }
    SpdyHeaders.setScheme(spdyVersion, spdySynStreamFrame, scheme);

    // Transfer the remaining HTTP headers
    for (Map.Entry<String, String> entry : httpMessage.getHeaders()) {
      spdySynStreamFrame.addHeader(entry.getKey(), entry.getValue());
    }

    if (chunked) {
      currentStreamID = streamID;
      spdySynStreamFrame.setLast(false);
    } else {
      spdySynStreamFrame.setLast(httpMessage.getContent().readableBytes() == 0);
    }

    return spdySynStreamFrame;
  }
Ejemplo n.º 13
0
 @Override
 public String uri() {
   return request.getUri();
 }
Ejemplo n.º 14
0
  /**
   * Call the appropriate handler for handling the httprequest. 404 if path is not found. 405 if
   * path is found but httpMethod does not match what's configured.
   *
   * @param request instance of {@code HttpRequest}
   * @param responder instance of {@code HttpResponder} to handle the request.
   * @return HttpMethodInfo object, null if urlRewriter rewrite returns false, also when method
   *     cannot be invoked.
   * @throws HandlerException If URL rewriting fails
   */
  public HttpMethodInfoBuilder getDestinationMethod(HttpRequest request, HttpResponder responder)
      throws HandlerException {
    if (urlRewriter != null) {
      try {
        request.setUri(URI.create(request.getUri()).normalize().toString());
        if (!urlRewriter.rewrite(request, responder)) {
          return null;
        }
      } catch (Throwable t) {
        log.error("Exception thrown during rewriting of uri {}", request.getUri(), t);
        throw new HandlerException(
            HttpResponseStatus.INTERNAL_SERVER_ERROR,
            String.format("Caught exception processing request. Reason: %s", t.getMessage()));
      }
    }

    String acceptHeaderStr = request.headers().get(HttpHeaders.Names.ACCEPT);
    List<String> acceptHeader =
        (acceptHeaderStr != null)
            ? Arrays.asList(acceptHeaderStr.split("\\s*,\\s*"))
                .stream()
                .map(mediaType -> mediaType.split("\\s*;\\s*")[0])
                .collect(Collectors.toList())
            : null;

    String contentTypeHeaderStr = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);
    // Trim specified charset since UTF-8 is assumed
    String contentTypeHeader =
        (contentTypeHeaderStr != null) ? contentTypeHeaderStr.split("\\s*;\\s*")[0] : null;

    try {
      String path = URI.create(request.getUri()).normalize().getPath();

      List<PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel>>
          routableDestinations = patternRouter.getDestinations(path);

      List<PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel>> matchedDestinations =
          getMatchedDestination(routableDestinations, request.getMethod(), path);

      if (!matchedDestinations.isEmpty()) {
        PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel> matchedDestination =
            matchedDestinations
                .stream()
                .filter(
                    matchedDestination1 -> {
                      return matchedDestination1
                              .getDestination()
                              .matchConsumeMediaType(contentTypeHeader)
                          && matchedDestination1
                              .getDestination()
                              .matchProduceMediaType(acceptHeader);
                    })
                .findFirst()
                .get();
        HttpResourceModel httpResourceModel = matchedDestination.getDestination();

        // Call preCall method of handler interceptors.
        boolean terminated = false;
        ServiceMethodInfo serviceMethodInfo =
            new ServiceMethodInfo(
                httpResourceModel.getMethod().getDeclaringClass().getName(),
                httpResourceModel.getMethod());
        for (Interceptor interceptor : interceptors) {
          if (!interceptor.preCall(request, responder, serviceMethodInfo)) {
            // Terminate further request processing if preCall returns false.
            terminated = true;
            break;
          }
        }

        // Call httpresource handle method, return the HttpMethodInfo Object.
        if (!terminated) {
          // Wrap responder to make post hook calls.
          responder = new WrappedHttpResponder(responder, interceptors, request, serviceMethodInfo);
          return HttpMethodInfoBuilder.getInstance()
              .httpResourceModel(httpResourceModel)
              .httpRequest(request)
              .httpResponder(responder)
              .requestInfo(
                  matchedDestination.getGroupNameValues(), contentTypeHeader, acceptHeader);
        }
      } else if (!routableDestinations.isEmpty()) {
        // Found a matching resource but could not find the right HttpMethod so return 405
        throw new HandlerException(HttpResponseStatus.METHOD_NOT_ALLOWED, request.getUri());
      } else {
        throw new HandlerException(
            HttpResponseStatus.NOT_FOUND,
            String.format("Problem accessing: %s. Reason: Not Found", request.getUri()));
      }
    } catch (NoSuchElementException ex) {
      throw new HandlerException(
          HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE,
          String.format("Problem accessing: %s. Reason: Unsupported Media Type", request.getUri()),
          ex);
    }
    return null;
  }
 @Override
 public boolean isHandledBy(HttpRequest req) {
   return req.getUri().startsWith(uriPrefix);
 }
Ejemplo n.º 16
0
 @Override
 public String getUrl() {
   return req.getUri();
 }
Ejemplo n.º 17
0
  @Override
  public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpRequest) {
      HttpRequest request = this.request = (HttpRequest) msg;
      URI uri = new URI(request.getUri());
      if (!uri.getPath().startsWith("/form")) {
        // Write Menu
        writeMenu(ctx);
        return;
      }
      responseContent.setLength(0);
      responseContent.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
      responseContent.append("===================================\r\n");

      responseContent.append("VERSION: " + request.getProtocolVersion().text() + "\r\n");

      responseContent.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n");
      responseContent.append("\r\n\r\n");

      // new getMethod
      for (Entry<String, String> entry : request.headers()) {
        responseContent.append("HEADER: " + entry.getKey() + '=' + entry.getValue() + "\r\n");
      }
      responseContent.append("\r\n\r\n");

      // new getMethod
      Set<Cookie> cookies;
      String value = request.headers().get(COOKIE);
      if (value == null) {
        cookies = Collections.emptySet();
      } else {
        cookies = CookieDecoder.decode(value);
      }
      for (Cookie cookie : cookies) {
        responseContent.append("COOKIE: " + cookie + "\r\n");
      }
      responseContent.append("\r\n\r\n");

      QueryStringDecoder decoderQuery = new QueryStringDecoder(request.getUri());
      Map<String, List<String>> uriAttributes = decoderQuery.parameters();
      for (Entry<String, List<String>> attr : uriAttributes.entrySet()) {
        for (String attrVal : attr.getValue()) {
          responseContent.append("URI: " + attr.getKey() + '=' + attrVal + "\r\n");
        }
      }
      responseContent.append("\r\n\r\n");

      // if GET Method: should not try to create a HttpPostRequestDecoder
      if (request.getMethod().equals(HttpMethod.GET)) {
        // GET Method: should not try to create a HttpPostRequestDecoder
        // So stop here
        responseContent.append("\r\n\r\nEND OF GET CONTENT\r\n");
        writeResponse(ctx.channel());
        return;
      }
      try {
        decoder = new HttpPostRequestDecoder(factory, request);
      } catch (ErrorDataDecoderException e1) {
        e1.printStackTrace();
        responseContent.append(e1.getMessage());
        writeResponse(ctx.channel());
        ctx.channel().close();
        return;
      }

      readingChunks = HttpHeaders.isTransferEncodingChunked(request);
      responseContent.append("Is Chunked: " + readingChunks + "\r\n");
      responseContent.append("IsMultipart: " + decoder.isMultipart() + "\r\n");
      if (readingChunks) {
        // Chunk version
        responseContent.append("Chunks: ");
        readingChunks = true;
      }
    }

    // check if the decoder was constructed before
    // if not it handles the form get
    if (decoder != null) {
      if (msg instanceof HttpContent) {
        // New chunk is received
        HttpContent chunk = (HttpContent) msg;
        try {
          decoder.offer(chunk);
        } catch (ErrorDataDecoderException e1) {
          e1.printStackTrace();
          responseContent.append(e1.getMessage());
          writeResponse(ctx.channel());
          ctx.channel().close();
          return;
        }
        responseContent.append('o');
        // example of reading chunk by chunk (minimize memory usage due to
        // Factory)
        readHttpDataChunkByChunk();
        // example of reading only if at the end
        if (chunk instanceof LastHttpContent) {
          writeResponse(ctx.channel());
          readingChunks = false;

          reset();
        }
      }
    }
  }