use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.
the class HttpResourceDispatcher method handleOptionsRequest.
private static void handleOptionsRequest(HTTPCarbonMessage cMsg, HttpService service) throws URITemplateException {
HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
if (cMsg.getHeader(HttpHeaderNames.ALLOW.toString()) != null) {
response.setHeader(HttpHeaderNames.ALLOW.toString(), cMsg.getHeader(HttpHeaderNames.ALLOW.toString()));
} else if (service.getBasePath().equals(cMsg.getProperty(HttpConstants.TO)) && !service.getAllAllowedMethods().isEmpty()) {
response.setHeader(HttpHeaderNames.ALLOW.toString(), DispatcherUtil.concatValues(service.getAllAllowedMethods(), false));
} else {
cMsg.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
throw new BallerinaConnectorException("no matching resource found for path : " + cMsg.getProperty(HttpConstants.TO) + " , method : " + "OPTIONS");
}
CorsHeaderGenerator.process(cMsg, response, false);
response.setProperty(HttpConstants.HTTP_STATUS_CODE, 200);
response.addHttpContent(new DefaultLastHttpContent());
HttpUtil.sendOutboundResponse(cMsg, response);
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.
the class HttpUtil method createErrorMessage.
public static HTTPCarbonMessage createErrorMessage(String payload, int statusCode) {
HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
response.waitAndReleaseAllEntities();
if (payload != null) {
payload = lowerCaseTheFirstLetter(payload);
response.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(payload.getBytes())));
} else {
response.addHttpContent(new DefaultLastHttpContent());
}
setHttpStatusCodes(statusCode, response);
return response;
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.
the class ServiceTest method testGetStringAfterSetString.
@Test(description = "Test getString after setting string")
public void testGetStringAfterSetString() {
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Content-Type", TEXT_PLAIN));
HTTPTestRequest setStringrequestMsg = MessageUtils.generateHTTPMessage("/echo/setString", "POST", headers, null);
String stringresponseMsgPayload = "hello";
setStringrequestMsg.waitAndReleaseAllEntities();
setStringrequestMsg.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(stringresponseMsgPayload.getBytes())));
Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, setStringrequestMsg);
HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/getString", "GET");
HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
Assert.assertNotNull(responseMsg);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(responseMsg).getInputStream());
Assert.assertNotNull(responseMsgPayload);
Assert.assertEquals(responseMsgPayload, stringresponseMsgPayload);
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.
the class ServiceTest method testSetString.
@Test
public void testSetString() {
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Content-Type", TEXT_PLAIN));
HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/setString", "POST", headers, null);
requestMsg.waitAndReleaseAllEntities();
requestMsg.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer("hello".getBytes())));
HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
Assert.assertNotNull(responseMsg);
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project netty by netty.
the class HttpPostMultipartRequestDecoderBenchmark method testHighNumberChunks.
public double testHighNumberChunks(boolean big, boolean noDisk) {
String BOUNDARY = "01f136d9282f";
int size = 8 * 1024;
int chunkNumber = 64;
StringBuilder stringBuilder = new StringBuilder(size);
stringBuilder.setLength(size);
String data = stringBuilder.toString();
byte[] bodyStartBytes = ("--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg_id\"\n\n15200\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg1\"; filename=\"file1.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
byte[] bodyPartBigBytes = data.getBytes(CharsetUtil.UTF_8);
byte[] intermediaryBytes = ("\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg2\"; filename=\"file2.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
byte[] finalBigBytes = ("\n" + "--" + BOUNDARY + "--\n").getBytes(CharsetUtil.UTF_8);
ByteBuf firstBuf = Unpooled.wrappedBuffer(bodyStartBytes);
ByteBuf finalBuf = Unpooled.wrappedBuffer(finalBigBytes);
ByteBuf nextBuf;
if (big) {
nextBuf = Unpooled.wrappedBuffer(bodyPartBigBytes);
} else {
nextBuf = Unpooled.wrappedBuffer(intermediaryBytes);
}
DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "/up");
req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + BOUNDARY);
long start = System.nanoTime();
DefaultHttpDataFactory defaultHttpDataFactory = new DefaultHttpDataFactory(noDisk ? 1024 * 1024 : 16 * 1024);
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(defaultHttpDataFactory, req);
firstBuf.retain();
decoder.offer(new DefaultHttpContent(firstBuf));
firstBuf.release();
for (int i = 1; i < chunkNumber; i++) {
nextBuf.retain();
decoder.offer(new DefaultHttpContent(nextBuf));
nextBuf.release();
nextBuf.readerIndex(0);
}
finalBuf.retain();
decoder.offer(new DefaultLastHttpContent(finalBuf));
finalBuf.release();
while (decoder.hasNext()) {
InterfaceHttpData httpData = decoder.next();
}
while (finalBuf.refCnt() > 0) {
finalBuf.release();
}
while (nextBuf.refCnt() > 0) {
nextBuf.release();
}
while (finalBuf.refCnt() > 0) {
finalBuf.release();
}
long stop = System.nanoTime();
double time = (stop - start) / 1000000.0;
defaultHttpDataFactory.cleanAllHttpData();
defaultHttpDataFactory.cleanRequestHttpData(req);
decoder.destroy();
return time;
}
Aggregations