Search in sources :

Example 11 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project dubbo by alibaba.

the class HttpCommandDecoder method decode.

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];
            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            }
        }
    }
    return commandContext;
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) Attribute(io.netty.handler.codec.http.multipart.Attribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project moco by dreamhead.

the class FormsRequestExtractor method doExtractForms.

private ImmutableMap<String, String> doExtractForms(final HttpPostRequestDecoder decoder) throws IOException {
    List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
    Map<String, String> forms = newHashMap();
    for (InterfaceHttpData data : bodyHttpDatas) {
        if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
            Attribute attribute = (Attribute) data;
            forms.put(attribute.getName(), attribute.getValue());
        }
    }
    return copyOf(forms);
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData)

Example 13 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.

the class Http2ServerRequestImpl method handleEnd.

void handleEnd(MultiMap trailers) {
    ended = true;
    conn.reportBytesRead(bytesRead);
    if (postRequestDecoder != null) {
        try {
            postRequestDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
            while (postRequestDecoder.hasNext()) {
                InterfaceHttpData data = postRequestDecoder.next();
                if (data instanceof Attribute) {
                    Attribute attr = (Attribute) data;
                    try {
                        formAttributes().add(attr.getName(), attr.getValue());
                    } catch (Exception e) {
                        // Will never happen, anyway handle it somehow just in case
                        handleException(e);
                    }
                }
            }
        } catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
        // ignore this as it is expected
        } catch (Exception e) {
            handleException(e);
        } finally {
            postRequestDecoder.destroy();
        }
    }
    if (endHandler != null) {
        endHandler.handle(null);
    }
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) URISyntaxException(java.net.URISyntaxException) StreamResetException(io.vertx.core.http.StreamResetException) ClosedChannelException(java.nio.channels.ClosedChannelException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 14 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project android by JetBrains.

the class GoogleCrashTest method checkServerReceivesPostedData.

@Ignore
@Test
public void checkServerReceivesPostedData() throws Exception {
    String expectedReportId = "deadcafe";
    Map<String, String> attributes = new ConcurrentHashMap<>();
    myTestServer.setResponseSupplier(httpRequest -> {
        if (httpRequest.method() != HttpMethod.POST) {
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
        }
        HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(httpRequest);
        try {
            for (InterfaceHttpData httpData : requestDecoder.getBodyHttpDatas()) {
                if (httpData instanceof Attribute) {
                    Attribute attr = (Attribute) httpData;
                    attributes.put(attr.getName(), attr.getValue());
                }
            }
        } catch (IOException e) {
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
        } finally {
            requestDecoder.destroy();
        }
        return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(expectedReportId.getBytes(UTF_8)));
    });
    CrashReport report = CrashReport.Builder.createForException(ourIndexNotReadyException).setProduct("AndroidStudioTestProduct").setVersion("1.2.3.4").build();
    CompletableFuture<String> reportId = myReporter.submit(report);
    assertEquals(expectedReportId, reportId.get());
    // assert that the server get the expected data
    assertEquals("AndroidStudioTestProduct", attributes.get(GoogleCrash.KEY_PRODUCT_ID));
    assertEquals("1.2.3.4", attributes.get(GoogleCrash.KEY_VERSION));
    // Note: the exception message should have been elided
    assertEquals("com.intellij.openapi.project.IndexNotReadyException: <elided>\n" + STACK_TRACE, attributes.get(GoogleCrash.KEY_EXCEPTION_INFO));
    List<String> descriptions = Arrays.asList("2.3.0.0\n1.8.0_73-b02", "2.3.0.1\n1.8.0_73-b02");
    report = CrashReport.Builder.createForCrashes(descriptions).setProduct("AndroidStudioTestProduct").setVersion("1.2.3.4").build();
    attributes.clear();
    reportId = myReporter.submit(report);
    assertEquals(expectedReportId, reportId.get());
    // check that the crash count and descriptions made through
    assertEquals(descriptions.size(), Integer.parseInt(attributes.get("numCrashes")));
    assertEquals("2.3.0.0\n1.8.0_73-b02\n\n2.3.0.1\n1.8.0_73-b02", attributes.get("crashDesc"));
    Path testData = Paths.get(AndroidTestBase.getTestDataPath());
    List<String> threadDump = Files.readAllLines(testData.resolve(Paths.get("threadDumps", "1.txt")), UTF_8);
    report = CrashReport.Builder.createForPerfReport("td.txt", Joiner.on('\n').join(threadDump)).build();
    attributes.clear();
    reportId = myReporter.submit(report);
    assertEquals(expectedReportId, reportId.get());
    assertEquals(threadDump.stream().collect(Collectors.joining("\n")), attributes.get("td.txt"));
}
Also used : Path(java.nio.file.Path) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)

Example 15 with InterfaceHttpData

use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project riposte by Nike-Inc.

the class RequestInfoImplTest method getMultipartParts_returns_data_from_multipartData.

@Test
public void getMultipartParts_returns_data_from_multipartData() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    Whitebox.setInternalState(requestInfo, "isMultipart", true);
    requestInfo.isCompleteRequestWithAllChunks = true;
    HttpPostMultipartRequestDecoder multipartDataMock = mock(HttpPostMultipartRequestDecoder.class);
    List<InterfaceHttpData> dataListMock = mock(List.class);
    doReturn(dataListMock).when(multipartDataMock).getBodyHttpDatas();
    requestInfo.multipartData = multipartDataMock;
    // when
    List<InterfaceHttpData> result = requestInfo.getMultipartParts();
    // then
    assertThat(result, is(dataListMock));
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) Test(org.junit.Test)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)23 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)6 List (java.util.List)5 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)4 Test (org.junit.Test)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 File (java.io.File)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)2 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)2 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)2 EndOfDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)2 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 StreamResetException (io.vertx.core.http.StreamResetException)2