Showing posts with label http. Show all posts
Showing posts with label http. Show all posts

Saturday, June 11, 2022

RFC 9110 HTTP Semantics - a refresh and must-read for everyone working on HTTP based applications

RFC 9110 HTTP Semantics, authored by Fielding, Nottingham, and Reschke, was just published. This is a great refreshing summary of what we learned about the web protocol evolving over three versions. As it states, the core semantics of HTTP did not change. That core and the ideas behind it were described by Fielding in his thesis, and known as REST. 

The document is long, and I just went through it once. It is a great technical document in terms of precision and self-descriptive. 

The Hypertext Transfer Protocol (HTTP) is a family of stateless, application-level, request/response protocols that share a generic interface, extensible semantics, and self-descriptive messages to enable flexible interaction with network-based hypertext information systems.

What a precise description. 

 

Tuesday, March 17, 2015

request.get(...).pipe(response) is not RESTful

Request is a nice package for HTTP client programming on node.js. It provides API's that can directly stream. So you can do things like
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
When programming a service proxy, it is straightforward to do something like
app.get('/proxied/service', function (req, res) {
    request.get('http://the/real/service').pipe(res);
})
It is so neat and powerful. However, it is not RESTful, and in some cases it could cause errors that is hard to figure out. When the client of the proxied service gets the response, it always expects some representation from your service not the real service. When the representation of 'http://the/real/service' was sent to the client, the client will be confused. For example, the authorization header, the cache-control header, or the cookie header can all have side effects.

In an application I develop, I had a proxied service just like the above. It provided a list in JSON that was fetched from the other service hosted by a different application. It ran well in test and first a few month after released, until one day a user complained that he cannot access the list that should appear in the suggestion of a text input. I found the service was down. However, the user should still be able to type in whatever s/he thought was right, and then save it on the application. But the issue was that the application rejects such a request for authentication reason if the user's hand was not quick enough.  It took me a while trying to reproduce the problem on my browser, and then figure it out.

The problem was that the remote service was down, and the client's browser got a "net::ERR_EMPTY_RESPONSE" error that basically means the server closed the connection without sending any data. That was only first half of the story. Such an error will force the browser to clean up the cookies set by the application, and all following requests from the same page will be rejected. So the proper way is still to provide a timeout, and an error handler like
   request({
      url: service.device.url,
      timeout: 30 * 1000,
      ...
    }, function (err, response, resBody) {
      if (err) {
        return res.json(503, ...);
      }
     ...
    });

500, 502, 503 are good codes for this case.

Tuesday, October 8, 2013

Clone a resource in HTTP

In order to clone or copy a file, we execute a command like
cp a b
Given a file named a, the command creates a new file named b that has the same content as a. Similarly, we can clone a resource A. The client sends the following request to the server.
PUT /resources/B
...
{"source": "/resources/A"}
However, a client normally does not have the knowledge to properly name a new resource. A more reasonable alternative is the following request.
POST /resources
...
{"source": "/resources/A"}
All these are straightforward, but really smell like RPC. They did not follow the REST constraint of self-descriptive message. How could the server figure out /resources/A is what the client wants. What if the resource has been changed or deleted after the request was sent out?  The following approach may be better:
GET /resources/A
Then
POST /resources
...
the representation of /resources/A
What if the representation is huge? Maybe we can do the following:
GET /resources/A
or
HEAD /resources/A
Then
POST /resources
...
{"source": "/resources/A",
"ETag": "frompreviousrequest"}

Tuesday, August 21, 2012

Two leaky designs related to URL length limit

I discuss two cases of leaky design in this post. This first is Google Calendar, and the second is eBay POST caching.

Google Calendar

If you are using Google Calendar, you might have seen the broken droid with 414 code like this.

The reason for 414 is that the request URI is so long that the server refuses to handle. The maximum length of URL allowed by a server depends on its implementation. No matter it is 255 bytes or 2k characters or even 4k characters, it will reach the limit when URL is used to encoding a complicate request query parameters.

In the case of Google Calendar, when a user clicks the link to add events in GMail. An HTTP GET request is sent to Calender server with the details of the events encoded in the URL. For normal event often represented in iCalendar format, the URL can provide enough room for the data. Some event message might attach other extra text inside, when GMail tries to encode those into the request URI to Calendar, 414 might happen.

A possible solution can be a modified version of GMail that ensure a proper length of the URL with important information encoded inside. However, that is not a right solution. It is because the interface design between GMail and Google Calendar is not RESTful or simply does not follow the HTTP specification. HTTP GET is for retrieving the representation of a resource not for creating a new resource. A side effect of such a design is a duplicate even will be created every time a user clicks the same link and issues the same HTTP GET request.

A straightforward fix is to use POST to create a new event, and the details in the original email can then be easily included in the message body. 414 will never happen. In order to prevent creation of duplicated event, the server might need to verify if it is the same event that already existed on the same time slot.

If, in some cases, the designer really want to create a new resource by a GET request, the request URI should be able to identify the resource to be created. That is, instead return a 404, the server creates a new resource based on the request URI, and then returns the representation of that resource. For a server serves many concurrent clients that might request that URL at the same time, special care needs to be taken to prevent creation of duplication resource on the server and also holds the following requests until the resource representation is available. I developed such a service by using a Servlet Filter to do these tasks with Jetty continuation.

eBay POST caching

In this post, eBay engineer described their careful design of caching POST messages for eBay search. Two major motivation of this design is 1) to leverage the performance and scalability benefits of HTTP caching, and 2) to avoid the length limitation of HTTP request URI. I think the design is leaky because it might introduce extra special header to the message and very naturally not all the intermediaries can understand those headers and help the caching. The forward and reverse proxies in the diagram might only be the customized deployment of eBay.

As Akara Sucharitakul pointed out in his comment, a 303 can be used for the initial POST request,and then caching will work with the following GET. However, this does not reduce the round-trip time. My thought is to generate the MD5 key on the client via JavaScript, and then GET the resource identified by the key. If the user is lucky to be the first to issue such a request, a 404 will be returned, and then the client will do the real POST to create that resource on the server.

Friday, January 9, 2009

The reverse C10K problem

The original C10K problem studies how to provide reasonable service to 10,000 simultaneous clients or HTTP requests using a normal web server. I call the following problem the reverse C10K problem, or RC10K --- how to support 10,000 simultaneous outbound HTTP requests running on a web server. The RC10K problem can be found in scenarios like service orchestrations and server-side mashups. A server-side mashup needs to send several simultaneous HTTP requests to partner services for each inbound request.

Many approaches to improving the performance and scalability of HTTP servers can be applied to tackle the original C10K problem. However, whether these approaches can tackle the reverse C10K problem needs to be verified. My recent paper discussed the RC10K problem, and presented some experiment results got from a prototype mashup server application.