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 // Normally, the page is loaded from http or https, so not specifying a protocol
38 // will result in a (valid) protocol-relative url. However, this won't work if
39 // the protocol is something else, like 'file:'
40 var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
41
42 var protocol = opts.protocol || defaultProtocol
43 var host = opts.hostname || opts.host
44 var port = opts.port
45 var path = opts.path || '/'
46
47 // Necessary for IPv6 addresses
48 if (host && host.indexOf(':') !== -1)
49 host = '[' + host + ']'
50
51 // This may be a relative url. The browser should always be able to interpret it correctly.
52 opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
53 opts.method = (opts.method || 'GET').toUpperCase()
54 opts.headers = opts.headers || {}
55
56 // Also valid opts.auth, opts.mode
57
58 var req = new ClientRequest(opts)
59 if (cb)
60 req.on('response', cb)
61 return req
62}
63
64export function get(opts, cb) {
65 var req = request(opts, cb)
66 req.end()
67 return req
68}
69
70export function Agent() {}
71Agent.defaultMaxSockets = 4
72
73export var METHODS = [
74 'CHECKOUT',
75 'CONNECT',
76 'COPY',
77 'DELETE',
78 'GET',
79 'HEAD',
80 'LOCK',
81 'M-SEARCH',
82 'MERGE',
83 'MKACTIVITY',
84 'MKCOL',
85 'MOVE',
86 'NOTIFY',
87 'OPTIONS',
88 'PATCH',
89 'POST',
90 'PROPFIND',
91 'PROPPATCH',
92 'PURGE',
93 'PUT',
94 'REPORT',
95 'SEARCH',
96 'SUBSCRIBE',
97 'TRACE',
98 'UNLOCK',
99 'UNSUBSCRIBE'
100]
101export var STATUS_CODES = {
102 100: 'Continue',
103 101: 'Switching Protocols',
104 102: 'Processing', // RFC 2518, obsoleted by RFC 4918
105 200: 'OK',
106 201: 'Created',
107 202: 'Accepted',
108 203: 'Non-Authoritative Information',
109 204: 'No Content',
110 205: 'Reset Content',
111 206: 'Partial Content',
112 207: 'Multi-Status', // RFC 4918
113 300: 'Multiple Choices',
114 301: 'Moved Permanently',
115 302: 'Moved Temporarily',
116 303: 'See Other',
117 304: 'Not Modified',
118 305: 'Use Proxy',
119 307: 'Temporary Redirect',
120 400: 'Bad Request',
121 401: 'Unauthorized',
122 402: 'Payment Required',
123 403: 'Forbidden',
124 404: 'Not Found',
125 405: 'Method Not Allowed',
126 406: 'Not Acceptable',
127 407: 'Proxy Authentication Required',
128 408: 'Request Time-out',
129 409: 'Conflict',
130 410: 'Gone',
131 411: 'Length Required',
132 412: 'Precondition Failed',
133 413: 'Request Entity Too Large',
134 414: 'Request-URI Too Large',
135 415: 'Unsupported Media Type',
136 416: 'Requested Range Not Satisfiable',
137 417: 'Expectation Failed',
138 418: 'I\'m a teapot', // RFC 2324
139 422: 'Unprocessable Entity', // RFC 4918
140 423: 'Locked', // RFC 4918
141 424: 'Failed Dependency', // RFC 4918
142 425: 'Unordered Collection', // RFC 4918
143 426: 'Upgrade Required', // RFC 2817
144 428: 'Precondition Required', // RFC 6585
145 429: 'Too Many Requests', // RFC 6585
146 431: 'Request Header Fields Too Large', // RFC 6585
147 500: 'Internal Server Error',
148 501: 'Not Implemented',
149 502: 'Bad Gateway',
150 503: 'Service Unavailable',
151 504: 'Gateway Time-out',
152 505: 'HTTP Version Not Supported',
153 506: 'Variant Also Negotiates', // RFC 2295
154 507: 'Insufficient Storage', // RFC 4918
155 509: 'Bandwidth Limit Exceeded',
156 510: 'Not Extended', // RFC 2774
157 511: 'Network Authentication Required' // RFC 6585
158};
159
160export default {
161 request,
162 get,
163 Agent,
164 METHODS,
165 STATUS_CODES
166}