|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator to generate raw HTTP/1.1 request strings, |
| 4 | + * in accordance to the RFC 7230 (and RFC 7231) specifications. |
| 5 | + * |
| 6 | + * @author |
| 7 | + * @irvinlim |
| 8 | + * |
| 9 | + * For any questions or issues regarding the generated code snippet, please open an issue mentioning the author. |
| 10 | + */ |
| 11 | + |
| 12 | +'use strict' |
| 13 | + |
| 14 | +var CRLF = '\r\n' |
| 15 | +var CodeBuilder = require('../../helpers/code-builder') |
| 16 | +var util = require('util') |
| 17 | + |
| 18 | +/** |
| 19 | + * Request follows the request message format in accordance to RFC 7230, Section 3. |
| 20 | + * Each section is prepended with the RFC and section number. |
| 21 | + * See more at https://tools.ietf.org/html/rfc7230#section-3. |
| 22 | + */ |
| 23 | +module.exports = function (source, options) { |
| 24 | + var opts = Object.assign( |
| 25 | + { |
| 26 | + absoluteURI: false, |
| 27 | + autoContentLength: true, |
| 28 | + autoHost: true |
| 29 | + }, |
| 30 | + options |
| 31 | + ) |
| 32 | + |
| 33 | + // RFC 7230 Section 3. Message Format |
| 34 | + // All lines have no indentation, and should be terminated with CRLF. |
| 35 | + var code = new CodeBuilder('', CRLF) |
| 36 | + |
| 37 | + // RFC 7230 Section 5.3. Request Target |
| 38 | + // Determines if the Request-Line should use 'absolute-form' or 'origin-form'. |
| 39 | + // Basically it means whether the "http://domain.com" will prepend the full url. |
| 40 | + var requestUrl = opts.absoluteURI ? source.fullUrl : source.uriObj.path |
| 41 | + |
| 42 | + // RFC 7230 Section 3.1.1. Request-Line |
| 43 | + code.push('%s %s %s', source.method, requestUrl, source.httpVersion) |
| 44 | + |
| 45 | + // RFC 7231 Section 5. Header Fields |
| 46 | + Object.keys(source.allHeaders).forEach(function (key) { |
| 47 | + // Capitalize header keys, even though it's not required by the spec. |
| 48 | + var keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, function (x) { |
| 49 | + return x.toUpperCase() |
| 50 | + }) |
| 51 | + |
| 52 | + code.push( |
| 53 | + '%s', |
| 54 | + util.format('%s: %s', keyCapitalized, source.allHeaders[key]) |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + // RFC 7230 Section 5.4. Host |
| 59 | + // Automatically set Host header if option is on and on header already exists. |
| 60 | + if (opts.autoHost && Object.keys(source.allHeaders).indexOf('host') === -1) { |
| 61 | + code.push('Host: %s', source.uriObj.host) |
| 62 | + } |
| 63 | + |
| 64 | + // RFC 7230 Section 3.3.3. Message Body Length |
| 65 | + // Automatically set Content-Length header if option is on, postData is present and no header already exists. |
| 66 | + if ( |
| 67 | + opts.autoContentLength && |
| 68 | + source.postData.text && |
| 69 | + Object.keys(source.allHeaders).indexOf('content-length') === -1 |
| 70 | + ) { |
| 71 | + code.push( |
| 72 | + 'Content-Length: %d', |
| 73 | + Buffer.byteLength(source.postData.text, 'ascii') |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + // Add extra line after header section. |
| 78 | + code.blank() |
| 79 | + |
| 80 | + // Separate header section and message body section. |
| 81 | + var headerSection = code.join() |
| 82 | + var messageBody = '' |
| 83 | + |
| 84 | + // RFC 7230 Section 3.3. Message Body |
| 85 | + if (source.postData.text) { |
| 86 | + messageBody = source.postData.text |
| 87 | + } |
| 88 | + |
| 89 | + // RFC 7230 Section 3. Message Format |
| 90 | + // Extra CRLF separating the headers from the body. |
| 91 | + return headerSection + CRLF + messageBody |
| 92 | +} |
| 93 | + |
| 94 | +module.exports.info = { |
| 95 | + key: '1.1', |
| 96 | + title: 'HTTP/1.1', |
| 97 | + link: 'https://tools.ietf.org/html/rfc7230', |
| 98 | + description: 'HTTP/1.1 request string in accordance with RFC 7230' |
| 99 | +} |
0 commit comments