1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | import ClientRequest from './http-lib/request';
|
31 | import {parse} from 'url';
|
32 |
|
33 | export function request(opts, cb) {
|
34 | if (typeof opts === 'string')
|
35 | opts = parse(opts)
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
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 |
|
49 | if (host && host.indexOf(':') !== -1)
|
50 | host = '[' + host + ']'
|
51 |
|
52 |
|
53 | opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
|
54 | opts.method = (opts.method || 'GET').toUpperCase()
|
55 | opts.headers = opts.headers || {}
|
56 |
|
57 |
|
58 |
|
59 | var req = new ClientRequest(opts)
|
60 | if (cb)
|
61 | req.on('response', cb)
|
62 | return req
|
63 | }
|
64 |
|
65 | export function get(opts, cb) {
|
66 | var req = request(opts, cb)
|
67 | req.end()
|
68 | return req
|
69 | }
|
70 |
|
71 | export function Agent() {}
|
72 | Agent.defaultMaxSockets = 4
|
73 |
|
74 | export 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 | ]
|
102 | export var STATUS_CODES = {
|
103 | 100: 'Continue',
|
104 | 101: 'Switching Protocols',
|
105 | 102: 'Processing',
|
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',
|
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',
|
140 | 422: 'Unprocessable Entity',
|
141 | 423: 'Locked',
|
142 | 424: 'Failed Dependency',
|
143 | 425: 'Unordered Collection',
|
144 | 426: 'Upgrade Required',
|
145 | 428: 'Precondition Required',
|
146 | 429: 'Too Many Requests',
|
147 | 431: 'Request Header Fields Too Large',
|
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',
|
155 | 507: 'Insufficient Storage',
|
156 | 509: 'Bandwidth Limit Exceeded',
|
157 | 510: 'Not Extended',
|
158 | 511: 'Network Authentication Required'
|
159 | };
|
160 |
|
161 | export default {
|
162 | request,
|
163 | get,
|
164 | Agent,
|
165 | METHODS,
|
166 | STATUS_CODES
|
167 | }
|