Search in sources :

Example 11 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project riposte by Nike-Inc.

the class RequestInfoImplTest method getMultipartParts_works_as_expected_with_known_valid_data.

@DataProvider(value = { "false  |   false", "false  |   true", "true   |   false", "true   |   true" }, splitBy = "\\|")
@Test
public void getMultipartParts_works_as_expected_with_known_valid_data(boolean httpVersionIsNull, boolean httpMethodIsNull) throws IOException {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    Whitebox.setInternalState(requestInfo, "isMultipart", true);
    Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8);
    Whitebox.setInternalState(requestInfo, "protocolVersion", (httpVersionIsNull) ? null : HttpVersion.HTTP_1_1);
    Whitebox.setInternalState(requestInfo, "method", (httpMethodIsNull) ? null : HttpMethod.POST);
    requestInfo.isCompleteRequestWithAllChunks = true;
    requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8);
    requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER);
    // when
    List<InterfaceHttpData> result = requestInfo.getMultipartParts();
    // then
    assertThat(result, notNullValue());
    assertThat(result.size(), is(1));
    InterfaceHttpData data = result.get(0);
    assertThat(data, instanceOf(FileUpload.class));
    FileUpload fileUploadData = (FileUpload) data;
    assertThat(fileUploadData.getName(), is(KNOWN_MULTIPART_DATA_NAME));
    assertThat(fileUploadData.getFilename(), is(KNOWN_MULTIPART_DATA_FILENAME));
    assertThat(fileUploadData.getString(CharsetUtil.UTF_8), is(KNOWN_MULTIPART_DATA_ATTR_UUID));
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 13 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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 14 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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 15 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.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)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)24 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)7 ArrayList (java.util.ArrayList)7 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