http.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. this and http-lib folder
  3. The MIT License
  4. Copyright (c) 2015 John Hiesey
  5. Permission is hereby granted, free of charge,
  6. to any person obtaining a copy of this software and
  7. associated documentation files (the "Software"), to
  8. deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify,
  10. merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom
  12. the Software is furnished to do so,
  13. subject to the following conditions:
  14. The above copyright notice and this permission notice
  15. shall be included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  20. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. import ClientRequest from './http-lib/request';
  25. import {parse} from 'url';
  26. export function request(opts, cb) {
  27. if (typeof opts === 'string')
  28. opts = parse(opts)
  29. // Normally, the page is loaded from http or https, so not specifying a protocol
  30. // will result in a (valid) protocol-relative url. However, this won't work if
  31. // the protocol is something else, like 'file:'
  32. var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
  33. var protocol = opts.protocol || defaultProtocol
  34. var host = opts.hostname || opts.host
  35. var port = opts.port
  36. var path = opts.path || '/'
  37. // Necessary for IPv6 addresses
  38. if (host && host.indexOf(':') !== -1)
  39. host = '[' + host + ']'
  40. // This may be a relative url. The browser should always be able to interpret it correctly.
  41. opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
  42. opts.method = (opts.method || 'GET').toUpperCase()
  43. opts.headers = opts.headers || {}
  44. // Also valid opts.auth, opts.mode
  45. var req = new ClientRequest(opts)
  46. if (cb)
  47. req.on('response', cb)
  48. return req
  49. }
  50. export function get(opts, cb) {
  51. var req = request(opts, cb)
  52. req.end()
  53. return req
  54. }
  55. export function Agent() {}
  56. Agent.defaultMaxSockets = 4
  57. export var METHODS = [
  58. 'CHECKOUT',
  59. 'CONNECT',
  60. 'COPY',
  61. 'DELETE',
  62. 'GET',
  63. 'HEAD',
  64. 'LOCK',
  65. 'M-SEARCH',
  66. 'MERGE',
  67. 'MKACTIVITY',
  68. 'MKCOL',
  69. 'MOVE',
  70. 'NOTIFY',
  71. 'OPTIONS',
  72. 'PATCH',
  73. 'POST',
  74. 'PROPFIND',
  75. 'PROPPATCH',
  76. 'PURGE',
  77. 'PUT',
  78. 'REPORT',
  79. 'SEARCH',
  80. 'SUBSCRIBE',
  81. 'TRACE',
  82. 'UNLOCK',
  83. 'UNSUBSCRIBE'
  84. ]
  85. export var STATUS_CODES = {
  86. 100: 'Continue',
  87. 101: 'Switching Protocols',
  88. 102: 'Processing', // RFC 2518, obsoleted by RFC 4918
  89. 200: 'OK',
  90. 201: 'Created',
  91. 202: 'Accepted',
  92. 203: 'Non-Authoritative Information',
  93. 204: 'No Content',
  94. 205: 'Reset Content',
  95. 206: 'Partial Content',
  96. 207: 'Multi-Status', // RFC 4918
  97. 300: 'Multiple Choices',
  98. 301: 'Moved Permanently',
  99. 302: 'Moved Temporarily',
  100. 303: 'See Other',
  101. 304: 'Not Modified',
  102. 305: 'Use Proxy',
  103. 307: 'Temporary Redirect',
  104. 400: 'Bad Request',
  105. 401: 'Unauthorized',
  106. 402: 'Payment Required',
  107. 403: 'Forbidden',
  108. 404: 'Not Found',
  109. 405: 'Method Not Allowed',
  110. 406: 'Not Acceptable',
  111. 407: 'Proxy Authentication Required',
  112. 408: 'Request Time-out',
  113. 409: 'Conflict',
  114. 410: 'Gone',
  115. 411: 'Length Required',
  116. 412: 'Precondition Failed',
  117. 413: 'Request Entity Too Large',
  118. 414: 'Request-URI Too Large',
  119. 415: 'Unsupported Media Type',
  120. 416: 'Requested Range Not Satisfiable',
  121. 417: 'Expectation Failed',
  122. 418: 'I\'m a teapot', // RFC 2324
  123. 422: 'Unprocessable Entity', // RFC 4918
  124. 423: 'Locked', // RFC 4918
  125. 424: 'Failed Dependency', // RFC 4918
  126. 425: 'Unordered Collection', // RFC 4918
  127. 426: 'Upgrade Required', // RFC 2817
  128. 428: 'Precondition Required', // RFC 6585
  129. 429: 'Too Many Requests', // RFC 6585
  130. 431: 'Request Header Fields Too Large', // RFC 6585
  131. 500: 'Internal Server Error',
  132. 501: 'Not Implemented',
  133. 502: 'Bad Gateway',
  134. 503: 'Service Unavailable',
  135. 504: 'Gateway Time-out',
  136. 505: 'HTTP Version Not Supported',
  137. 506: 'Variant Also Negotiates', // RFC 2295
  138. 507: 'Insufficient Storage', // RFC 4918
  139. 509: 'Bandwidth Limit Exceeded',
  140. 510: 'Not Extended', // RFC 2774
  141. 511: 'Network Authentication Required' // RFC 6585
  142. };
  143. export default {
  144. request,
  145. get,
  146. Agent,
  147. METHODS,
  148. STATUS_CODES
  149. }