Sample server to support 100-continue - http

I started implementing a sample server (in netty) to support 100 continue.
I got some confusion when I go thrugh the RCF2616 section 8.2.3. It says
Upon receiving a request which includes an Expect request-header
field with the "100-continue" expectation, an origin server MUST
either respond with 100 (Continue) status and continue to read
from the input stream, or respond with a final status code. The
origin server MUST NOT wait for the request body before sending
the 100 (Continue) response. If it responds with a final status
code, it MAY close the transport connection or it MAY continue
to read and discard the rest of the request. It MUST NOT
perform the requested method if it returns a final status code.
What does it mean by The origin server MUST NOT wait for the request body before sending the 100 (Continue) response.
Should my server first validate the headers and then send the 100 (continue) status code or Immediately send the 100 status code ?
Please clarify me the actual behavior of a http server to support 100 continue
Currently this is my channelRead
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
request = req;
if (req.getMethod() != HttpMethod.POST) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
boolean valid = false;
for (Map.Entry<String, String> header : req.headers()) {
if (header.getKey().equals("my-special-header")) {
valid = true;
break;
}
}
if (!valid) {
FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST);
ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
} else {
if (HttpHeaders.is100ContinueExpected(request)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
}
}
} else if (msg instanceof LastHttpContent && msg != LastHttpContent.EMPTY_LAST_CONTENT) {
DefaultLastHttpContent content = (DefaultLastHttpContent) msg;
System.out.println("content read");
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content.content());
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
boolean keepAlive =HttpHeaders.isKeepAlive(request);
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
ctx.write(response);
}
}
}

I think "MUST NOT wait for the request body" is pretty clear. The body does not include thee headers, so I'm not sure where your confusion comes from...

Related

HowTo set response status code in CXF SOAP OneWay request on error

I have implemented a #OneWay JAX-RS service with Apache CXF ( a dropwizard application ). When called with invalid structure, causing an unmarshalling error in DocLiteralInInterceptor, http status code 200 is returned to client. To make the calling process recognize the fault, I need to return status 400 or 500, along with the error text from Unmarshalling Error.
I recognized that, after the error, the "in" interceptor chain is unwound ( interceptors handleFault-methods are called in reverse order ), so I installed an interceptor at the start of the "in"-chain ( last on unwinding ) with
public CustomSOAPInterceptor(String chainname) {
super(Phase.RECEIVE);
getBefore().add(PolicyInInterceptor.class.getName());
this.chainname=chainname;
}
Within my handleFault-Method I can seperate the fault message and recognize the unmarshall error. But I am not succeeding in setting the response.
I tried
Fault f = (Fault) e;
f.setStatusCode(Response.Status.BAD_REQUEST.getStatusCode());
and
Response response = Response
.status(Response.Status.BAD_REQUEST)
.entity(Response.Status.BAD_REQUEST.getStatusCode() + " " + f.getLocalizedMessage())
.build();
soapMessage.getExchange().put(Response.class, response);
and
message.put(Message.RESPONSE_CODE, Response.Status.BAD_REQUEST.getStatusCode());
Where is the response set and how can I overwrite it ?
Tx for any advice.
I know, its kind of late, but for those who are looking for a solution:
In my application, the following works:
public void handleFault(SoapMessage soapMessage) {
/* some code to test for specific error deleted */
Exchange exchange = soapMessage.getExchange();
Message outMessage = exchange.getOutMessage();
if (outMessage == null) {
Endpoint endpoint = exchange.get(Endpoint.class);
outMessage = endpoint.getBinding().createMessage();
exchange.setOutMessage(outMessage);
}
try {
EndpointReferenceType target = exchange.get(EndpointReferenceType.class);
Conduit conduit = exchange.getDestination().getBackChannel(soapMessage);
exchange.setConduit(conduit);
conduit.prepare(outMessage);
} catch (IOException ex) {
LOG.error(ex.getMessage(), ex);
}
Object resp = outMessage.get("HTTP.RESPONSE");
if (resp != null && resp instanceof HttpServletResponse) {
HttpServletResponse response = (HttpServletResponse) resp;
response.setStatus(Response.Status.BAD_REQUEST.getStatusCode());
}
soapMessage.getInterceptorChain().abort();
}

HTTP requests stops working after some time

I have strange problem with my Game written in Unity (Mono). I have login functionality and then, after successfull login i'm sending some kind of keep-alive requests to ensure token is updated (I'm sending it every 30s).
The problem is, that after some time (sometimes it's 1 hour, sometimes 2.5 hours) all my requests have timeout status.
To be sure about my connection status I made some checks in code: I'm making simple GET to http://google.com and to main website of my API (not an API call. Just website GET). When I got timeout on API next time i figured out that:
After 1 timeout I have it always. Restarting application helps
I'm getting timeouts on API call and on GET request for base API website
Google is still responding with status 200 (no timeouts here)
Implementation:
On beginning i've been using RestSharp to handle requests, but the problem occured and there was a decision to throw RestSharp away and now we're using classic WebClient
class BetterWebClient : WebClient
{
private WebRequest _Request = null;
public TimeSpan? Timeout { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
this._Request = base.GetWebRequest(address);
if ( Timeout.HasValue )
{
_Request.Timeout = (int)Timeout.Value.TotalMilliseconds;
}
else
{
_Request.Timeout = 10*1000; //10s
}
if (this._Request is HttpWebRequest)
{
((HttpWebRequest)this._Request).AllowAutoRedirect = true;
}
return this._Request;
}
}
My HandleRequest function (which also calls Google and API website) looks like this:
public static void HandleRequest<TR>(string url, RestResponse<TR> executeGetRequest) where TR : new()
{
using (BetterWebClient w = new BetterWebClient() {Timeout = TimeSpan.FromSeconds(2)})
{
try
{
string downloadString = w.DownloadString("http://www.google.com");
Debug.Log("Google request: OK");
}
catch ( Exception )
{
Debug.LogError("Google request failed");
}
try
{
string downloadString = w.DownloadString("myAPIwebsite");
Debug.Log("WorldOfRescue.com request: OK");
}
catch ( Exception )
{
Debug.LogError("WorldOfRescue.com request failed");
}
}
var client = new BetterWebClient() {Timeout = TimeSpan.FromSeconds(5)};
client.Headers.Add("Accept", "application/json");
client.Headers.Add("Content-Type", "application/json");
try
{
string downloadString = client.DownloadString(url);
Debug.Log("Request for "+url+ " completed. Response: " + downloadString);
}
catch ( WebException e )
{
Debug.Log(e.Status);
throw;
}
catch ( Exception e )
{
Debug.Log(e.ToString());
throw;
}
finally
{
client.Dispose();
}
}
Do you have any idea why it happens? It looks like something is blocking me to send request to specific website, but in same time other website is working fine.

How should I implement the HEAD response for a dynamically generated resource?

Below code is written with Spring MVC. I simulate the dynamic response generation by reading a file first and send it to client.
For a GET method, the response will contain the Transfer-Encoding: chunked header rather than the Content-Length header.
For a HEAD method, how should I implement the response? Should I manually insert the Transfer-Encoding: chunked header and remove the Content-Length header?
#RestController
public class ChunkedTransferAPI {
#Autowired
ServletContext servletContext;
#RequestMapping(value = "xxx.iso", method = { RequestMethod.GET })
public void doChunkedGET(HttpServletResponse response) {
String filename = "/xxx.iso";
try {
ServletOutputStream output = response.getOutputStream();
InputStream input = servletContext.getResourceAsStream(filename);
BufferedInputStream bufferedInput = new BufferedInputStream(input);
int datum = bufferedInput.read();
while (datum != -1) {
output.write(datum); //data transfer happens here.
datum = bufferedInput.read();
}
output.flush();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
#RequestMapping(value = "xxx.iso", method = { RequestMethod.HEAD })
public void doChunkedHEAD(HttpServletResponse response) {
// response.setHeader("Server", "Apache-Coyote/1.1");
// response.setHeader("Transfer-Encoding", "chunked");
}
}
My client's behavior is:
Initiate a HEAD request first to get the anticipated response size. This size is used to allocate some buffer.
Then initiate a GET request to actually get the response content and put it in the buffer.
I kind of have the feeling that I am catering to the client's behavior rather than following some RFC standard. I am worried that even if I can make the client happy with my response, it will fail with other servers' responses.
Anyone could shed some light on this? How should I implement the HEAD response?
Or maybe the client should NEVER rely on the HEAD response to decide the size of a GET response because the RFC says:
The server SHOULD send the same header fields in response to a HEAD
request as it would have sent if the request had been a GET, except
that the payload header fields (Section 3.3) MAY be omitted.
And Content-Length happens to be one of the payload header fields.

WebException timeout vs HttpWebResponse timeout?

I'm using the Asp.Net WebClient to create an HTTP post.
The below code has try-catch block around the code which catches WebException:
try
{
using (MyWebClient wc = new MyWebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = _lender.ContentType;
wc.Timeout = 200;
return _lender.GetResult(wc.UploadString(_lender.PostUri, _lender.PostValues));
}
}
catch (WebException ex)
{
return new ServiceError(ex.Status.ToString());
}
The main exceptions I'm looking for are timeouts. I've extended WebClient to allow me to set the timeout.
When I set the timeout to say 100ms, an exception is thrown as expected. I can get the WebException status as per the example (it returns "timeout"), however, I want to return status codes too.
If I extract the httpwebresponse using ex.Response I get a null value returned, when I was expecting an associated status code.
Why do I not get an HttpStatus.Request.Timeout?
I have the same problem and I realise a few things while I search for a solution.
WebExceptionStatus enum is not equivalent to http status code that the API you call returned. Instead it is a enum of possible error that may occour during a http call.
The WebExceptionStatus error code that will be returned when you receive an error (400 to 599) from your API is WebExceptionStatus.ProtocolError aka number 7 as int.
When you need to get the response body or the real http status code returned from the api, first you need to check if WebException.Status is WebExceptionStatus.ProtocolError. Then you can get the real response from WebExceptionStatus.Response and read its content.
Sometimes the timeout is handled by the caller (aka your code) so you do not have a response in that case. So you can look if WebException.Status is WebExceptionStatus.Timeout
This is an example:
try
{
...
}
catch (WebException webException)
{
if (webException.Status == WebExceptionStatus.ProtocolError)
{
var httpResponse = (HttpWebResponse)webException.Response;
var responseText = "";
using (var content = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = content.ReadToEnd(); // Get response body as text
}
int statusCode = (int)httpResponse.StatusCode; // Get the status code
}
else if (webException.Status == WebExceptionStatus.ProtocolError)
{
// Timeout handled by your code. You do not have a response here.
}
// Handle other webException.Status errors. You do not have a response here.
}

Is there a way to allow Google App Engine to send body or payload with a DELETE request?

I'm attempting to interface with an API that requires XML data to be contained in the body of an HTTP DELETE request. I'm using urlfetch in AppEngine and the payload is simply ignored for DELETE requests.
After reading this article: Is an entity body allowed for an HTTP DELETE request?, I realize that the standard probably doesn't allow body content on DELETE requests and that's why urlfetch is stripping the body.
So my question is: is there some sort of work-around to append body content in app engine when urlfetch ignores the payload?
Per the docs,
The URL fetch service supports five
HTTP methods: GET, POST, HEAD, PUT and
DELETE. The request can include HTTP
headers, and body content for a POST
or PUT request.
Given that the GAE Python runtime is heavily sandboxed, it's extremely unlikely that you'll be able to get around this restriction. I consider that to be a bug, and you should probably file a bug report here.
You can make DELETE request with body via sockets, sample Java code that checks HTTPRequest and does different request for DELETE with body:
public static HTTPResponse execute(HTTPRequest request) throws ExecutionException, InterruptedException {
if (request == null) {
throw new IllegalArgumentException("Missing request!");
}
if (request.getMethod() == HTTPMethod.DELETE && request.getPayload() != null && request.getPayload().length > 0) {
URL obj = request.getURL();
SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
try {
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
con.setRequestMethod("DELETE");
for (HTTPHeader httpHeader : request.getHeaders()) {
con.setRequestProperty(httpHeader.getName(), httpHeader.getValue());
}
con.setDoOutput(true);
con.setDoInput(true);
OutputStream out = con.getOutputStream();
out.write(request.getPayload());
out.flush();
out.close();
List<HTTPHeader> responseHeaders = new ArrayList<>();
for (Map.Entry<String, List<String>> stringListEntry : con.getHeaderFields().entrySet()) {
for (String value : stringListEntry.getValue()) {
responseHeaders.add(new HTTPHeader(stringListEntry.getKey(), value));
}
}
return new HTTPResponse(con.getResponseCode(), StreamUtils.getBytes(con.getInputStream()), con.getURL(), responseHeaders);
} catch (IOException e) {
log.severe(e.getMessage());
}
} else {
Future<HTTPResponse> future = URLFetchServiceFactory.getURLFetchService().fetchAsync(request);
return future.get();
}
return null;
}
You can get around this using the App Engine Socket API, here is how that looks in Go:
client := http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return socket.Dial(c, network, addr)
},
},
}

Resources