Difference between revisions of "HTTPRequest"

From Fernseher
Jump to navigationJump to search
 
Line 16: Line 16:
*query: the request query
*query: the request query
*host: the request host
*host: the request host
*version: the request version
*protocol: the request protocol version
*client: the requesting client ip
*client: the requesting client ip
*prime: indicates whether this is the "prime" request, that is, the one that started the script up and needs to be quasi-special.
*prime: indicates whether this is the "prime" request, that is, the one that started the script up and needs to be quasi-special.

Latest revision as of 22:35, 22 January 2009

The HTTPRequest class represents an HTTP Request message for my Modjs environment. It contains a request line and other information about the requestor, and a MIME entity (MimeEntity) for the request headers and data. Its primary use will be for the script writer to see the request from the browser to the server, and what data they are sending over. It will do a little bit to parse out cookie variables, query variables, and post data variables to make the script writer's life easier. It can also be tied back to a HTTPResponse object to make inclusion of cookie headers mostly seemless when new cookies are defined and the response is sent back with them.

When a Modjs script builds a completely new HTTPRequest object in order to send it over a TCPConnection or the like to a different, remote, server, it will behave a little bit differently, in that, rather than parsing the headers and request data in order to get cookies/queries/posts, it will have the ability to unparse those variables sets back into headers and request data, as needed (and only if requested). And not cookies. Just too much work for too little benefit.

To create a new HTTPRequest object, the constructor is called:

request = new HTTPRequest(host [,method [,path [,query [,version]]]])

Note that except for host, the rest of the arguments could be passed as one string. They are essentially joined and reparsed to verify that the correct syntax is used for the request line. host sets the required Host: header in the mimeentity on creation. If the method is POST, the Content-type: header also gets set to the default value of application/form-data.

This could throw an exception if method or query string are malformed:

HTTPRequestException

The object will have these properties:

  • method: the request method
  • path: the request path
  • query: the request query
  • host: the request host
  • protocol: the request protocol version
  • client: the requesting client ip
  • prime: indicates whether this is the "prime" request, that is, the one that started the script up and needs to be quasi-special.
  • entity: the MimeEntity for the data in this request, including headers.
  • queryVars: the query is parsed and proper variables are put in here as name/value pairs. Last one sticks. If you reuse a name, you'll have to go to the real query to get both values. On outgoing requests, this will get slurped up and urlencoded and _added_ to the query property as part of the query string in the request line. Be aware!
  • cookieVars: the Cookie and Cookie2 headers are parsed and proper variables are put in here as name/value pairs. If there are duplicate names, the last one sticks. Go to the actual headers if you need more, or property values. Note that this is not a magic cookie jar. For the outgoing response, if you need to add new cookies, you must add the cookie using the HTTPResponse addCookie() method. For outgoing requests, the cookie variables placed in here do not get magically merged into the appropriate Cookie: header, since it doesn't have enough information.
  • postVars: If the method is POST and the MIME entity is correct, posted variables get parsed and put in here as name/value pairs. If there are dups, again, the last one sticks. You may have to actually decode the entity if you need more details. On outgoing requests, this will get slurped up and put as variables in the POST data, in the appropriate section, depending on current content-type and method, etc.

The object has a couple methods, toString() which essentially converts to a single string, appropriate for sending over a TCPConnection. This will do queryVars and postVars recombination, if there are any variables there. Be aware! It will also use proper CRLF EOLs for headers (I think?). Also, getRequestString() which combines the request information and returns the request line in the current state based on properties and query variables.

string = request.toString()
string = request.getRequestString()