private boolean exitAfterSpecialCases( final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future) throws Exception { HttpRequest httpRequest = future.getNettyRequest().getHttpRequest(); ProxyServer proxyServer = future.getProxyServer(); int statusCode = response.getStatus().code(); Request request = future.getCurrentRequest(); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (statusCode == UNAUTHORIZED_401) { return exitAfterHandling401( channel, future, response, request, statusCode, realm, proxyServer, httpRequest); } else if (statusCode == PROXY_AUTHENTICATION_REQUIRED_407) { return exitAfterHandling407( channel, future, response, request, statusCode, proxyServer, httpRequest); } else if (statusCode == CONTINUE_100) { return exitAfterHandling100(channel, future, statusCode); } else if (REDIRECT_STATUSES.contains(statusCode)) { return exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm); } else if (httpRequest.getMethod() == HttpMethod.CONNECT && statusCode == OK_200) { return exitAfterHandlingConnect( channel, future, request, proxyServer, statusCode, httpRequest); } return false; }
private boolean handleConnectOKAndExit( int statusCode, Realm realm, final Request request, HttpRequest httpRequest, HttpResponse response, final NettyResponseFuture<?> future, ProxyServer proxyServer, final Channel channel) throws IOException { if (statusCode == OK.code() && httpRequest.getMethod() == HttpMethod.CONNECT) { LOGGER.debug("Connected to {}:{}", proxyServer.getHost(), proxyServer.getPort()); if (future.isKeepAlive()) { future.attachChannel(channel, true); } try { LOGGER.debug("Connecting to proxy {} for scheme {}", proxyServer, request.getUrl()); channels.upgradeProtocol(channel.pipeline(), request.getURI().getScheme()); } catch (Throwable ex) { channels.abort(future, ex); } future.setReuseChannel(true); future.setConnectAllowed(false); requestSender.sendNextRequest(new RequestBuilder(future.getRequest()).build(), future); return true; } return false; }
@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(); } } }
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()); }
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()); } }
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception { // Allow only GET methods. if (req.getMethod() != GET) { sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.getWebSocketLocation(req), null, false); this.handshaker = wsFactory.newHandshaker(ctx, req); if (this.handshaker == null) { wsFactory.sendUnsupportedWebSocketVersionResponse(ctx); } else { this.handshaker.performOpeningHandshake(ctx, req); } }
@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; }
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; }
@Override public String getMethod() { return req.getMethod().name(); }
@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(); } } } }
/** * 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; }