Friday, May 4, 2012

nodejs : Anatomy of http.request

http.request() method will create createRequest Object.This will inturn call Agent's addRequestMethod.
This will see if there is any already created socket for host:port combination.If the no of sockets created for this combination is < than the set configuration max limit value, a socket is created using net.createconnection.After the socket is returned createRequestObject's onSocket method is called which will create a function and add it to the event queue to be executed as part of next tick.This created function will set up socket's ondata.

sockets's ondata function creates a parser and this parser parses the data and takes necessary actions.This parser creates a IncomingMessage object and populates the response.on complete it will emit response event on createRequest Object. This will call the callback we passed while initiating the request(http.request)

clientResponse is a http.IncomingMessage object, an eventEmitter that gets built and populated by the parser whose execute method is called when socket data is available,
   
    the callback we are passing gets called after the headers are processed, at which time we will add listeners for the events 'data' & 'end'.
       
    Parser while parsing the message body will emit 'data' event on clientResponse.
    Parser will emit 'end' event on messageComplete

No comments:

Post a Comment