index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //"version": "2.8.2",
  2. var ClientRequest = require('./lib/request')
  3. var response = require('./lib/response')
  4. var extend = require('xtend')
  5. var statusCodes = require('builtin-status-codes')
  6. var url = require('url')
  7. var http = exports
  8. http.request = function (opts, cb) {
  9. if (typeof opts === 'string')
  10. opts = url.parse(opts)
  11. else
  12. opts = extend(opts)
  13. // Normally, the page is loaded from http or https, so not specifying a protocol
  14. // will result in a (valid) protocol-relative url. However, this won't work if
  15. // the protocol is something else, like 'file:'
  16. var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
  17. var protocol = opts.protocol || defaultProtocol
  18. var host = opts.hostname || opts.host
  19. var port = opts.port
  20. var path = opts.path || '/'
  21. // Necessary for IPv6 addresses
  22. if (host && host.indexOf(':') !== -1)
  23. host = '[' + host + ']'
  24. // This may be a relative url. The browser should always be able to interpret it correctly.
  25. opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
  26. opts.method = (opts.method || 'GET').toUpperCase()
  27. opts.headers = opts.headers || {}
  28. // Also valid opts.auth, opts.mode
  29. var req = new ClientRequest(opts)
  30. if (cb)
  31. req.on('response', cb)
  32. return req
  33. }
  34. http.get = function get (opts, cb) {
  35. var req = http.request(opts, cb)
  36. req.end()
  37. return req
  38. }
  39. http.ClientRequest = ClientRequest
  40. http.IncomingMessage = response.IncomingMessage
  41. http.Agent = function () {}
  42. http.Agent.defaultMaxSockets = 4
  43. http.globalAgent = new http.Agent()
  44. http.STATUS_CODES = statusCodes
  45. http.METHODS = [
  46. 'CHECKOUT',
  47. 'CONNECT',
  48. 'COPY',
  49. 'DELETE',
  50. 'GET',
  51. 'HEAD',
  52. 'LOCK',
  53. 'M-SEARCH',
  54. 'MERGE',
  55. 'MKACTIVITY',
  56. 'MKCOL',
  57. 'MOVE',
  58. 'NOTIFY',
  59. 'OPTIONS',
  60. 'PATCH',
  61. 'POST',
  62. 'PROPFIND',
  63. 'PROPPATCH',
  64. 'PURGE',
  65. 'PUT',
  66. 'REPORT',
  67. 'SEARCH',
  68. 'SUBSCRIBE',
  69. 'TRACE',
  70. 'UNLOCK',
  71. 'UNSUBSCRIBE'
  72. ]