public static String getPostParameter(HttpRequest req, String name) { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder( new DefaultHttpDataFactory(false), req); InterfaceHttpData data = decoder.getBodyHttpData(name); if (data.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) data; String value = null; try { value = attribute.getValue(); } catch (IOException e) { e.printStackTrace(); } return value; } return null; }
/** * request parameters put in {@link #parameters} * * @param req */ private void requestParametersHandler(HttpRequest req) { if (req.method().equals(HttpMethod.POST)) { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req); try { List<InterfaceHttpData> postList = decoder.getBodyHttpDatas(); for (InterfaceHttpData data : postList) { List<String> values = new ArrayList<String>(); MixedAttribute value = (MixedAttribute) data; value.setCharset(CharsetUtil.UTF_8); values.add(value.getValue()); this.parameters.put(data.getName(), values); } } catch (Exception e) { logger.error(e.getMessage()); } } }
void copyHttpBodyData(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){ ByteBuf bbContent = fullHttpReq.content(); if(bbContent.hasArray()) { servletRequest.setContent(bbContent.array()); } else { if(fullHttpReq.getMethod().equals(HttpMethod.POST)){ HttpPostRequestDecoder decoderPostData = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), fullHttpReq); String bbContentStr = bbContent.toString(Charset.forName(UTF_8)); servletRequest.setContent(bbContentStr.getBytes()); if( ! decoderPostData.isMultipart() ){ List<InterfaceHttpData> postDatas = decoderPostData.getBodyHttpDatas(); for (InterfaceHttpData postData : postDatas) { if (postData.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) postData; try { servletRequest.addParameter(attribute.getName(),attribute.getValue()); } catch (IOException e) { e.printStackTrace(); } } } } } } }
private void readHeaders(ChannelHandlerContext ctx, HttpRequest msg) { if(req == null && context == null) { context = new HttpBridletContextImpl(); req = new HttpBridletRequestImpl( msg ); QueryStringDecoder decoderQuery = new QueryStringDecoder(msg.getUri()); req.setQueryString(decoderQuery.parameters()); req.setCookies(parseCookies(msg.headers().get(COOKIE))); // new getMethod if(req.isForm()) { decoder = new HttpPostRequestDecoder(FACTORY, msg); } } else { sendBadRequest(ctx); } }
private void decodePost() { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request); // Form Data List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas(); try { for (InterfaceHttpData data : bodyHttpDatas) { if (data.getHttpDataType().equals(HttpDataType.Attribute)) { Attribute attr = (MixedAttribute) data; params.put(data.getName(), new Param(Arrays.asList(attr.getValue()))); } } } catch (IOException e) { throw new RuntimeException(e); } decoder.destroy(); }
public void setHttpRequest(HttpRequest request) throws BadRequestException, IOException { if (request == null) { LOG.error("HttpRequest not initialized"); throw new BadRequestException("HttpRequest not initialized"); } if (!request.getMethod().equals(HttpMethod.POST)) { LOG.error("Got invalid HTTP method: expecting only POST"); throw new BadRequestException("Incorrect method " + request.getMethod().toString() + ", expected POST"); } HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, request); InterfaceHttpData data = decoder.getBodyHttpData(HTTP_TEST_ATTRIBUTE); if (data == null) { LOG.error("HTTP Resolve request inccorect, {} attribute not found", HTTP_TEST_ATTRIBUTE); throw new BadRequestException("HTTP Resolve request inccorect, " + HTTP_TEST_ATTRIBUTE + " attribute not found"); } Attribute attribute = (Attribute) data; requestData = attribute.get(); LOG.trace("Name {}, type {} found, data size {}", data.getName(), data.getHttpDataType().name(), requestData.length); }
private Map<String, List<String>> parseQueryParams() { // query string final QueryStringDecoder query = new QueryStringDecoder(uri()); final Map<String, List<String>> queryParams = new HashMap<>(query.parameters()); //TODO multipart/form-data if (!"application/x-www-form-urlencoded".equalsIgnoreCase(contentType())) return queryParams; // http body final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request); final List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas(); bodyHttpDatas.stream() .parallel() .filter(e -> e.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) .map(e -> (Attribute) e) .map(e -> { try { return new AbstractMap.SimpleImmutableEntry<String, String>(e.getName(), e.getValue()); } catch (IOException ex) { throw new RuntimeException(ex); } }) .forEach(e -> { String key = e.getKey(); if (!queryParams.containsKey(key)) queryParams.putIfAbsent(key, new ArrayList<>(1)); queryParams.get(key).add(e.getValue()); }); return queryParams; }
public RequestInfoImpl(HttpRequest request) { this(request.getUri(), request.getMethod(), request.headers(), HttpUtils.extractTrailingHeadersIfPossible(request), null, HttpUtils.extractCookies(request), null, HttpUtils.extractContentChunks(request), request.getProtocolVersion(), HttpHeaders.isKeepAlive(request), (request instanceof FullHttpRequest), HttpPostRequestDecoder.isMultipart(request)); }
private void readContent(ChannelHandlerContext ctx, HttpContent msg) throws IOException { if(req != null) { if(req.isForm()) { try { decoder.offer(msg); readHttpDataChunkByChunk(); } catch(HttpPostRequestDecoder.EndOfDataDecoderException ex) { handleRequest(ctx); return; } catch (DecoderException e) { LOG.log(Level.WARNING, e.getMessage()); sendBadRequest(ctx); return; } } else { req.setContent(msg.content()); } //if is the last http content if(msg instanceof LastHttpContent) { handleRequest(ctx); } } else { sendBadRequest(ctx); } }
private void executePostValues(SeedRequest request,HttpPostRequestDecoder decoder){ List<InterfaceHttpData> params = decoder.getBodyHttpDatas(); for(InterfaceHttpData data :params){ MixedAttribute attribute = (MixedAttribute) data; try { request.setValue(new String(attribute.getName()), URLDecoder.decode(new String(attribute.content().array()),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; SeedRequest request = new SeedRequest(); SeedSession session = ctx.attr(SESSION_FLAG).get(); SeedResponse response = new SeedResponse(session); try{ if(req instanceof FullHttpRequest){ if(req.getMethod().equals(HttpMethod.POST)){ ByteBuf buf = ((FullHttpRequest) req).content(); byte[] data = buf.array(); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req); executePostValues(request,decoder); request.setBody(data); } URI uri = new URI(req.getUri()); for(Map.Entry<String, String> header:req.headers().entries()){ request.setHeader(header.getKey(),header.getValue()); } request.setMethod(req.getMethod().name()); UriUtil.executeUri(request, uri.getQuery()); web.execute(uri.getPath(),request,response); } }catch (Exception e){ //400 if(response!=null) { response.write("response code 400".getBytes(), "text/html", HttpResponseCode.CODE_400.getCode()); response.flush(); } } finally{ // if(response!=null && !response.isWrite()) // response.writeSuccess(); } } }
private String domainKey(FullHttpRequest request) throws IOException { FullHttpRequest copy = request.copy(); ReferenceCountUtil.releaseLater(copy); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(copy); List<InterfaceHttpData> keyDatas = decoder.getBodyHttpDatas("domain_key"); assertThat(keyDatas, is(not(nullValue()))); assertThat(keyDatas.size(), is(1)); InterfaceHttpData domainKeyData = keyDatas.get(0); assertThat(domainKeyData.getHttpDataType(), is(HttpDataType.Attribute)); return ((Attribute) domainKeyData).getValue(); }
public GaleRequest process(FullHttpRequest request) { GaleRequest result = new GaleRequest(); result.setUri(request.getUri()); result.setMethod(request.getMethod()); result.setHeaders(request.headers()); result.setVersion(request.getProtocolVersion()); //parse query parameters QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri(), CharsetUtil.UTF_8); result.setPath(queryDecoder.path()); result.getParameters().putAll(queryDecoder.parameters()); //parse body parameters HttpPostRequestDecoder bodyDecoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), request); List<InterfaceHttpData> datum = bodyDecoder.getBodyHttpDatas(); if (datum != null && !datum.isEmpty()) { for (InterfaceHttpData data : datum) { String name = data.getName(); String value = null; if (data.getHttpDataType().equals(HttpDataType.Attribute)) { //do not parse file data Attribute attribute = (Attribute)data; try { value = attribute.getString(CharsetUtil.UTF_8); result.getParameters().add(name, value); } catch(Exception e) { ELOGGER.error(this.getClass().getName(), e); } } } } bodyDecoder.destroy(); return result; }
public URIDecoder(HttpRequest httpRequest, Map<String, String> extractedParams) { super(httpRequest.uri()); this.paths = path().split("/"); if (httpRequest.method() == HttpMethod.PUT || httpRequest.method() == HttpMethod.POST) { if (httpRequest instanceof HttpContent) { this.contentType = httpRequest.headers().get(HttpHeaderNames.CONTENT_TYPE); if (contentType != null && contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) { this.decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), httpRequest); } else { this.bodyData = ((HttpContent) httpRequest).content(); } } } this.pathData = extractedParams; }
/** * {@inheritDoc} * <p/> * Prepares the request for reading by decoding all the content added via {@link #addContent(HttpContent)}. * @throws RestServiceException if request channel is closed or if the request could not be decoded/prepared. */ @Override public void prepare() throws RestServiceException { if (!isOpen()) { nettyMetrics.multipartRequestAlreadyClosedError.inc(); throw new RestServiceException("Request is closed", RestServiceErrorCode.RequestChannelClosed); } else if (!readyForRead) { // make sure data is held in memory. HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false); HttpPostMultipartRequestDecoder postRequestDecoder = new HttpPostMultipartRequestDecoder(httpDataFactory, request); try { HttpContent httpContent = rawRequestContents.poll(); while (httpContent != null) { try { // if the request is also an instance of HttpContent, the HttpPostMultipartRequestDecoder does the offer // automatically at the time of construction. We should not add it again. if (httpContent != request) { postRequestDecoder.offer(httpContent); } } finally { ReferenceCountUtil.release(httpContent); } httpContent = rawRequestContents.poll(); } for (InterfaceHttpData part : postRequestDecoder.getBodyHttpDatas()) { processPart(part); } requestContents.add(LastHttpContent.EMPTY_LAST_CONTENT); readyForRead = true; } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) { nettyMetrics.multipartRequestDecodeError.inc(); throw new RestServiceException("There was an error decoding the request", e, RestServiceErrorCode.MalformedRequest); } finally { postRequestDecoder.destroy(); } } }
@Override public void messageReceived(ChannelHandlerContext ctx, Env env) throws Exception { if (env.getRequest().getMethod() == POST || env.getRequest().getMethod() == PUT || env.getRequest().getMethod() == PATCH) { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(env.getRequest()); for (InterfaceHttpData entry: decoder.getBodyHttpDatas()) { if (entry.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute)entry; env.getRequest().setParam((String)attribute.getName(), (String)attribute.getValue()); } } } nextHandler(ctx, env); }
FormParams(HttpPostRequestDecoder decoder) { this.decoder = decoder; }
@Override public void initialize(ChannelHandlerContext ctx, HttpRequest request) { this.decoder = new HttpPostRequestDecoder(request); }
@Override public void parse() throws Exception { LOG.trace("CommandName: " + COMMAND_NAME + ": Parse.."); HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, getRequest()); if (decoder.isMultipart()) { LOG.trace("Chunked: " + HttpHeaders.isTransferEncodingChunked(getRequest())); LOG.trace(": Multipart.."); List<InterfaceHttpData> datas = decoder.getBodyHttpDatas(); if (!datas.isEmpty()) { for (InterfaceHttpData data : datas) { LOG.trace("Multipart1 name " + data.getName() + " type " + data.getHttpDataType().name()); if (data.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) data; if (CommonEpConstans.REQUEST_SIGNATURE_ATTR_NAME.equals(data.getName())) { requestSignature = attribute.get(); if (LOG.isTraceEnabled()) { LOG.trace("Multipart name " + data.getName() + " type " + data.getHttpDataType().name() + " Signature set. size: " + requestSignature.length); LOG.trace(MessageEncoderDecoder.bytesToHex(requestSignature)); } } else if (CommonEpConstans.REQUEST_KEY_ATTR_NAME.equals(data.getName())) { requestKey = attribute.get(); if (LOG.isTraceEnabled()) { LOG.trace("Multipart name " + data.getName() + " type " + data.getHttpDataType().name() + " requestKey set. size: " + requestKey.length); LOG.trace(MessageEncoderDecoder.bytesToHex(requestKey)); } } else if (CommonEpConstans.REQUEST_DATA_ATTR_NAME.equals(data.getName())) { requestData = attribute.get(); if (LOG.isTraceEnabled()) { LOG.trace("Multipart name " + data.getName() + " type " + data.getHttpDataType().name() + " requestData set. size: " + requestData.length); LOG.trace(MessageEncoderDecoder.bytesToHex(requestData)); } } else if (CommonEpConstans.NEXT_PROTOCOL_ATTR_NAME.equals(data.getName())) { nextProtocol = ByteBuffer.wrap(attribute.get()).getInt(); LOG.trace("[{}] next protocol is {}", getSessionUuid(), nextProtocol); } } } } else { LOG.error("Multipart.. size 0"); throw new BadRequestException("HTTP Request inccorect, multiprat size is 0"); } } }
/** * Handles a {@link HttpRequest}. * <p/> * Does some state maintenance for all HTTP methods by creating a {@link RestRequest} wrapping this * {@link HttpRequest} * <p/> * In case of POST, delegates handling of {@link RestRequest} to the {@link RestRequestHandler}. * @param httpRequest the {@link HttpRequest} that needs to be handled. * @return {@code true} if the handling succeeded without problems. * @throws RestServiceException if there is an error handling the current {@link HttpRequest}. */ private boolean handleRequest(HttpRequest httpRequest) throws RestServiceException { boolean success = true; if (responseChannel == null || requestContentFullyReceived) { // Once all content associated with a request has been received, this channel is clear to receive new requests. // If the client sends a request without waiting for the response, it is possible to screw things up a little // but doing so would constitute an error and no proper client would do that. long processingStartTime = System.currentTimeMillis(); resetState(); nettyMetrics.requestArrivalRate.mark(); if (!httpRequest.decoderResult().isSuccess()) { success = false; logger.warn("Decoder failed because of malformed request on channel {}", ctx.channel(), httpRequest.decoderResult().cause()); nettyMetrics.malformedRequestError.inc(); onRequestAborted(new RestServiceException("Decoder failed because of malformed request", RestServiceErrorCode.MalformedRequest)); } else { try { // We need to maintain state about the request itself for the subsequent parts (if any) that come in. We will // attach content to the request as the content arrives. if ((HttpMethod.POST.equals(httpRequest.method()) || HttpMethod.PUT.equals(httpRequest.method())) && HttpPostRequestDecoder.isMultipart(httpRequest)) { nettyMetrics.multipartPostRequestRate.mark(); request = new NettyMultipartRequest(httpRequest, ctx.channel(), nettyMetrics, nettyConfig.nettyBlacklistedQueryParams, nettyConfig.nettyMultipartPostMaxSizeBytes); } else { request = new NettyRequest(httpRequest, ctx.channel(), nettyMetrics, nettyConfig.nettyBlacklistedQueryParams); } responseChannel.setRequest(request); logger.trace("Channel {} now handling request {}", ctx.channel(), request.getUri()); // We send POST that is not multipart for handling immediately since we expect valid content with it that will // be streamed in. In the case of POST that is multipart, all the content has to be received for Netty's // decoder and NettyMultipartRequest to work. So it is scheduled for handling when LastHttpContent is received. // With any other method that we support, we do not expect any valid content. LastHttpContent is a Netty thing. // So we wait for LastHttpContent (throw an error if we don't receive it or receive something else) and then // schedule the other methods for handling in handleContent(). if ((request.getRestMethod().equals(RestMethod.POST) || request.getRestMethod().equals(RestMethod.PUT)) && !HttpPostRequestDecoder.isMultipart(httpRequest)) { requestHandler.handleRequest(request, responseChannel); } } catch (RestServiceException e) { success = false; onRequestAborted(e); } finally { if (request != null) { request.getMetricsTracker().nioMetricsTracker.addToRequestProcessingTime( System.currentTimeMillis() - processingStartTime); } } } } else { // We have received a request when we were not expecting one. This shouldn't happen and the channel is closed // because it is in a bad state. success = false; logger.error("New request received when previous request is yet to be fully received on channel {}. Request under" + " processing: {}. Unexpected request: {}", ctx.channel(), request.getUri(), httpRequest.uri()); nettyMetrics.duplicateRequestError.inc(); onRequestAborted(new RestServiceException("Received request in the middle of another request", RestServiceErrorCode.BadRequest)); } return success; }
/** * Creates a {@link HttpPostRequestDecoder} of the request body. * * @return a {@link HttpPostRequestDecoder} */ public HttpPostRequestDecoder parseBody() { return new HttpPostRequestDecoder(new DefaultHttpDataFactory(Short.MAX_VALUE), req); }
/** * Provides info on whether this request is multipart or not. * @return {@code true} if multipart. {@code false} otherwise. */ protected boolean isMultipart() { return HttpPostRequestDecoder.isMultipart(request); }