private void write(XHROptionsMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) {
    HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);

    HttpHeaders.addHeader(res, "Set-Cookie", "io=" + msg.getSessionId());
    HttpHeaders.addHeader(res, CONNECTION, KEEP_ALIVE);
    HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_HEADERS, CONTENT_TYPE);
    addOriginHeaders(ctx.channel(), res);

    ByteBuf out = encoder.allocateBuffer(ctx.alloc());
    sendMessage(msg, ctx.channel(), out, res, promise);
  }
  private void addOriginHeaders(Channel channel, HttpResponse res) {
    if (version != null) {
      res.headers().add(HttpHeaders.Names.SERVER, version);
    }

    if (configuration.getOrigin() != null) {
      HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getOrigin());
      HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
    } else {
      String origin = channel.attr(ORIGIN).get();
      if (origin != null) {
        HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_ORIGIN, origin);
        HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
      } else {
        HttpHeaders.addHeader(res, ACCESS_CONTROL_ALLOW_ORIGIN, "*");
      }
    }
  }
  @Override
  public void writeToResponse(SessionContext context) {
    FullHttpResponse response = context.getResponse();
    if (detector.hasHeader(response, name)) {
      response.headers().remove(name);
    }

    HttpHeaders.addHeader(response, name, new String(resource.readFor(context.getRequest())));
  }
  @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);
    }
  }
Esempio n. 5
0
 public static void setTransferEncodingChunked(HttpMessage m) {
   addHeader(m, Names.TRANSFER_ENCODING, Values.CHUNKED);
   removeHeader(m, Names.CONTENT_LENGTH);
 }