THTTPResponse

THTTPResponse

new THTTPResponse()

Structure for direct write HTTP response. Passed as parameter to application level scripting methods (see App.registerEndpoint ) also accessible in in entity level scripting methods while in rest mode.

Implements string writer.

Caller MUST call writeEnd once in the end and set response statusCode

To send file content as a response without loading file into memory the following code can be used:

  // Replace this comments by JSDocs style comment
  // param {THTTPRequest} req
  // param {THTTPResponse} resp
  function getPublicDocument(req, resp){
    resp.statusCode = 200;
    resp.writeHead('Content-Type: !STATICFILE\r\nContent-Type: text/plain'); // !STATICFILE is a special content type - will be removed from headers by server during sending
    resp.writeEnd('c:\\myFilesWithPasswords.txt');
  }
Implements:

Members

statusCode: Number

response HTTP status code

Methods

writeHead(header)

Add response header(s). Can be called several times for DIFFERENT header

resp.writeHead('Content-Type: text/css; charset=UTF-8\r\nOther-header: value')

Arguments:
  1. header (String)  one header or #13#10 separated headers

writeBinaryBase64(base64Data)

Write base64 encoded data as a binary representation (will decode from base64 to binary before write to response)
Arguments:
  1. base64Data (String)

writeEnd(data, encodingopt)

Write to internal buffer and set buffer content as HTTP response. See {UBWriter.wrote} for parameter details
Arguments:
  1. data (ArrayBuffer|Object|String)
  2. [encoding] (String)

validateETag()

Perform a ETag based HTTP response caching. Must be called after writeEnd called and and statusCode is defined.

In case statusCode === 2000 and response body length > 64 will

  • if request contains a IF-NONE-MATCH header, and it value equal to response crc32 will mutate a statusCode to 304 (not modified) and clear the response body
  • in other case will add a ETag header with value = hex representation of crc32(responseBody).

write(data, encodingopt)

Write to source.
Arguments:
  1. data (ArrayBuffer|Object|String)  Data to write. If Object - it stringify before write
  2. [encoding] (String)  Encode data to encoding before write. Default to utf-8 in case data is String or bin in case data is ArrayBuffer. One of "utf-8"|"ucs2"|"bin"|"base64".
Implements: