UNPKG

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