/
jswrap_http.c
601 lines (552 loc) · 18.2 KB
/
jswrap_http.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains JavaScript HTTP Functions
* ----------------------------------------------------------------------------
*/
#include "jswrap_net.h"
#include "jswrap_http.h"
#include "jsvariterator.h"
#include "socketserver.h"
#include "../network.h"
/*JSON{
"type" : "library",
"class" : "http"
}
This library allows you to create http servers and make http requests
In order to use this, you will need an extra module to get network connectivity
such as the [TI CC3000](/CC3000) or [WIZnet W5500](/WIZnet).
This is designed to be a cut-down version of the [node.js
library](http://nodejs.org/api/http.html). Please see the [Internet](/Internet)
page for more information on how to use it.
*/
/*JSON{
"type" : "class",
"library" : "http",
"class" : "httpSrv"
}
The HTTP server created by `require('http').createServer`
*/
// there is a 'connect' event on httpSrv, but it's used by createServer and isn't node-compliant
/*JSON{
"type" : "class",
"library" : "http",
"class" : "httpSRq"
}
The HTTP server request
*/
/*JSON{
"type" : "event",
"class" : "httpSRq",
"name" : "data",
"params" : [
["data","JsVar","A string containing one or more characters of received data"]
]
}
The 'data' event is called when data is received. If a handler is defined with
`X.on('data', function(data) { ... })` then it will be called, otherwise data
will be stored in an internal buffer, where it can be retrieved with `X.read()`
*/
/*JSON{
"type" : "event",
"class" : "httpSRq",
"name" : "close"
}
Called when the connection closes.
*/
/*JSON{
"type" : "property",
"class" : "httpSRq",
"name" : "headers",
"generate" : false,
"return" : ["JsVar", "An object mapping header name to value" ]
}
The headers to sent to the server with this HTTP request.
*//*Documentation only*/
/*JSON{
"type" : "property",
"class" : "httpSRq",
"name" : "method",
"generate" : false,
"return" : ["JsVar", "A string" ]
}
The HTTP method used with this request. Often `"GET"`.
*//*Documentation only*/
/*JSON{
"type" : "property",
"class" : "httpSRq",
"name" : "url",
"generate" : false,
"return" : ["JsVar", "A string representing the URL" ]
}
The URL requested in this HTTP request, for instance:
* `"/"` - the main page
* `"/favicon.ico"` - the web page's icon
*//*Documentation only*/
/*JSON{
"type" : "method",
"class" : "httpSRq",
"name" : "available",
"generate" : "jswrap_stream_available",
"return" : ["int","How many bytes are available"]
}
Return how many bytes are available to read. If there is already a listener for
data, this will always return 0.
*/
/*JSON{
"type" : "method",
"class" : "httpSRq",
"name" : "read",
"generate" : "jswrap_stream_read",
"params" : [
["chars","int","The number of characters to read, or undefined/0 for all available"]
],
"return" : ["JsVar","A string containing the required bytes."]
}
Return a string containing characters that have been received
*/
/*JSON{
"type" : "method",
"class" : "httpSRq",
"name" : "pipe",
"ifndef" : "SAVE_ON_FLASH",
"generate" : "jswrap_pipe",
"params" : [
["destination","JsVar","The destination file/stream that will receive content from the source."],
["options","JsVar",["[optional] An object `{ chunkSize : int=32, end : bool=true, complete : function }`","chunkSize : The amount of data to pipe from source to destination at a time","complete : a function to call when the pipe activity is complete","end : call the 'end' function on the destination when the source is finished"]]
],
"typescript": "pipe(dest: any, options?: PipeOptions): void"
}
Pipe this to a stream (an object with a 'write' method)
*/
/*JSON{
"type" : "class",
"library" : "http",
"class" : "httpSRs"
}
The HTTP server response
*/
/*JSON{
"type" : "event",
"class" : "httpSRs",
"name" : "drain"
}
An event that is fired when the buffer is empty and it can accept more data to
send.
*/
/*JSON{
"type" : "event",
"class" : "httpSRs",
"name" : "close"
}
Called when the connection closes.
*/
/*JSON{
"type" : "class",
"library" : "http",
"class" : "httpCRq"
}
The HTTP client request, returned by `http.request()` and `http.get()`.
*/
/*JSON{
"type" : "event",
"class" : "httpCRq",
"name" : "drain"
}
An event that is fired when the buffer is empty and it can accept more data to
send.
*/
/*JSON{
"type" : "event",
"class" : "httpCRq",
"name" : "error"
}
An event that is fired if there is an error making the request and the response
callback has not been invoked. In this case the error event concludes the
request attempt. The error event function receives an error object as parameter
with a `code` field and a `message` field.
*/
/*JSON{
"type" : "class",
"library" : "http",
"class" : "httpCRs"
}
The HTTP client response, passed to the callback of `http.request()` an
`http.get()`.
*/
/*JSON{
"type" : "event",
"class" : "httpCRs",
"name" : "data",
"params" : [
["data","JsVar","A string containing one or more characters of received data"]
]
}
The 'data' event is called when data is received. If a handler is defined with
`X.on('data', function(data) { ... })` then it will be called, otherwise data
will be stored in an internal buffer, where it can be retrieved with `X.read()`
*/
/*JSON{
"type" : "event",
"class" : "httpCRs",
"name" : "close"
}
Called when the connection closes with one `hadError` boolean parameter, which
indicates whether an error occurred.
*/
/*JSON{
"type" : "event",
"class" : "httpCRs",
"name" : "error"
}
An event that is fired if there is an error receiving the response. The error
event function receives an error object as parameter with a `code` field and a
`message` field. After the error event the close even will also be triggered to
conclude the HTTP request/response.
*/
/*JSON{
"type" : "property",
"class" : "httpCRs",
"name" : "headers",
"generate" : false,
"return" : ["JsVar", "An object mapping header name to value" ]
}
The headers received along with the HTTP response
*//*Documentation only*/
/*JSON{
"type" : "property",
"class" : "httpCRs",
"name" : "statusCode",
"generate" : false,
"return" : ["JsVar", "The status code as a String" ]
}
The HTTP response's status code - usually `"200"` if all went well
*//*Documentation only*/
/*JSON{
"type" : "property",
"class" : "httpCRs",
"name" : "statusMessage",
"generate" : false,
"return" : ["JsVar", "An String Status Message" ]
}
The HTTP response's status message - Usually `"OK"` if all went well
*//*Documentation only*/
/*JSON{
"type" : "property",
"class" : "httpCRs",
"name" : "httpVersion",
"generate" : false,
"return" : ["JsVar", "Th" ]
}
The HTTP version reported back by the server - usually `"1.1"`
*//*Documentation only*/
/*JSON{
"type" : "method",
"class" : "httpCRs",
"name" : "available",
"generate" : "jswrap_stream_available",
"return" : ["int","How many bytes are available"]
}
Return how many bytes are available to read. If there is a 'data' event handler,
this will always return 0.
*/
/*JSON{
"type" : "method",
"class" : "httpCRs",
"name" : "read",
"generate" : "jswrap_stream_read",
"params" : [
["chars","int","The number of characters to read, or undefined/0 for all available"]
],
"return" : ["JsVar","A string containing the required bytes."]
}
Return a string containing characters that have been received
*/
/*JSON{
"type" : "method",
"class" : "httpCRs",
"name" : "pipe",
"ifndef" : "SAVE_ON_FLASH",
"generate" : "jswrap_pipe",
"params" : [
["destination","JsVar","The destination file/stream that will receive content from the source."],
["options","JsVar",["[optional] An object `{ chunkSize : int=32, end : bool=true, complete : function }`","chunkSize : The amount of data to pipe from source to destination at a time","complete : a function to call when the pipe activity is complete","end : call the 'end' function on the destination when the source is finished"]]
],
"typescript": "pipe(destination: any, options?: PipeOptions): void"
}
Pipe this to a stream (an object with a 'write' method)
*/
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "staticmethod",
"class" : "http",
"name" : "createServer",
"generate" : "jswrap_http_createServer",
"params" : [
["callback","JsVar","A function(request,response) that will be called when a connection is made"]
],
"return" : ["JsVar","Returns a new httpSrv object"],
"return_object" : "httpSrv"
}
Create an HTTP Server
When a request to the server is made, the callback is called. In the callback
you can use the methods on the response (`httpSRs`) to send data. You can also
add `request.on('data',function() { ... })` to listen for POSTed data
*/
JsVar *jswrap_http_createServer(JsVar *callback) {
JsVar *skippedCallback = jsvSkipName(callback);
if (!jsvIsFunction(skippedCallback)) {
jsError("Expecting Callback Function but got %t", skippedCallback);
jsvUnLock(skippedCallback);
return 0;
}
jsvUnLock(skippedCallback);
return serverNew(ST_HTTP, callback);
}
/*JSON{
"type" : "staticmethod",
"class" : "http",
"name" : "request",
"generate_full" : "jswrap_net_connect(options, callback, ST_HTTP)",
"params" : [
["options","JsVar","An object containing host,port,path,method,headers fields (and also ca,key,cert if HTTPS is enabled)"],
["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
],
"return" : ["JsVar","Returns a new httpCRq object"],
"return_object" : "httpCRq"
}
Create an HTTP Request - `end()` must be called on it to complete the operation.
`options` is of the form:
```
var options = {
host: 'example.com', // host name
port: 80, // (optional) port, defaults to 80
path: '/', // path sent to server
method: 'GET', // HTTP command sent to server (must be uppercase 'GET', 'POST', etc)
protocol: 'http:', // optional protocol - https: or http:
headers: { key : value, key : value } // (optional) HTTP headers
};
var req = require("http").request(options, function(res) {
res.on('data', function(data) {
console.log("HTTP> "+data);
});
res.on('close', function(data) {
console.log("Connection closed");
});
});
// You can req.write(...) here if your request requires data to be sent.
req.end(); // called to finish the HTTP request and get the response
```
You can easily pre-populate `options` from a URL using `var options =
url.parse("http://www.example.com/foo.html")`
There's an example of using [`http.request` for HTTP POST
here](/Internet#http-post)
**Note:** if TLS/HTTPS is enabled, options can have `ca`, `key` and `cert`
fields. See `tls.connect` for more information about these and how to use them.
*/
/*JSON{
"type" : "staticmethod",
"class" : "http",
"name" : "get",
"generate" : "jswrap_http_get",
"params" : [
["options","JsVar","A simple URL, or an object containing host,port,path,method fields"],
["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
],
"return" : ["JsVar","Returns a new httpCRq object"],
"return_object" : "httpCRq"
}
Request a webpage over HTTP - a convenience function for `http.request()` that
makes sure the HTTP command is 'GET', and that calls `end` automatically.
```
require("http").get("http://pur3.co.uk/hello.txt", function(res) {
res.on('data', function(data) {
console.log("HTTP> "+data);
});
res.on('close', function(data) {
console.log("Connection closed");
});
});
```
See `http.request()` and [the Internet page](/Internet) and ` for more usage
examples.
*/
JsVar *jswrap_http_get(JsVar *options, JsVar *callback) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return 0;
if (jsvIsObject(options)) {
// if options is a string - it will be parsed, and GET will be set automatically
jsvObjectSetChildAndUnLock(options, "method", jsvNewFromString("GET"));
}
JsVar *skippedCallback = jsvSkipName(callback);
if (!jsvIsUndefined(skippedCallback) && !jsvIsFunction(skippedCallback)) {
jsError("Expecting Callback Function but got %t", skippedCallback);
jsvUnLock(skippedCallback);
return 0;
}
jsvUnLock(skippedCallback);
JsVar *cliReq = jswrap_net_connect(options, callback, ST_HTTP);
if (cliReq) clientRequestEnd(&net, cliReq);
networkFree(&net);
return cliReq;
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "method",
"class" : "httpSrv",
"name" : "listen",
"generate_full" : "jswrap_net_server_listen(parent, port, ST_HTTP)",
"params" : [
["port","int32","The port to listen on"]
],
"return" : ["JsVar","The HTTP server instance that 'listen' was called on"]
}
Start listening for new HTTP connections on the given port
*/
// Re-use existing
/*JSON{
"type" : "method",
"class" : "httpSrv",
"name" : "close",
"generate" : "jswrap_net_server_close"
}
Stop listening for new HTTP connections
*/
// Re-use existing
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "property",
"class" : "httpSRs",
"name" : "headers",
"generate" : false,
"return" : ["JsVar", "An object mapping header name to value" ]
}
The headers to send back along with the HTTP response.
The default contents are:
```
{
"Connection": "close"
}
```
*//*Documentation only*/
/*JSON{
"type" : "method",
"class" : "httpSRs",
"name" : "write",
"generate" : "jswrap_httpSRs_write",
"params" : [
["data","JsVar","A string containing data to send"]
],
"return" : ["bool","For node.js compatibility, returns the boolean false. When the send buffer is empty, a `drain` event will be sent"]
}
This function writes the `data` argument as a string. Data that is passed in
(including arrays) will be converted to a string with the normal JavaScript
`toString` method. For more information about sending binary data see
`Socket.write`
*/
bool jswrap_httpSRs_write(JsVar *parent, JsVar *data) {
serverResponseWrite(parent, data);
return false;
}
/*JSON{
"type" : "method",
"class" : "httpSRs",
"name" : "end",
"generate" : "jswrap_httpSRs_end",
"params" : [
["data","JsVar","A string containing data to send"]
]
}
See `Socket.write` for more information about the data argument
*/
void jswrap_httpSRs_end(JsVar *parent, JsVar *data) {
if (!jsvIsUndefined(data)) jswrap_httpSRs_write(parent, data);
serverResponseEnd(parent);
}
/*JSON{
"type" : "method",
"class" : "httpSRs",
"name" : "writeHead",
"generate" : "jswrap_httpSRs_writeHead",
"params" : [
["statusCode","int32","The HTTP status code"],
["headers","JsVar","An object containing the headers"]
]
}
Send the given status code and headers. If not explicitly called this will be
done automatically the first time data is written to the response.
This cannot be called twice, or after data has already been sent in the
response.
*/
void jswrap_httpSRs_writeHead(JsVar *parent, int statusCode, JsVar *headers) {
serverResponseWriteHead(parent, statusCode, headers);
}
/*JSON{
"type" : "method",
"class" : "httpSRs",
"name" : "setHeader",
"generate" : "jswrap_httpSRs_setHeader",
"params" : [
["name","JsVar","The name of the header as a String"],
["value","JsVar","The value of the header as a String"]
]
}
Set a value to send in the header of this HTTP response. This updates the
`httpSRs.headers` property.
Any headers supplied to `writeHead` will overwrite any headers with the same
name.
*/
void jswrap_httpSRs_setHeader(JsVar *parent, JsVar *name, JsVar *value) {
serverResponseSetHeader(parent, name, value);
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "method",
"class" : "httpCRq",
"name" : "write",
"generate" : "jswrap_net_socket_write",
"params" : [
["data","JsVar","A string containing data to send"]
],
"return" : ["bool","For node.js compatibility, returns the boolean false. When the send buffer is empty, a `drain` event will be sent"]
}
This function writes the `data` argument as a string. Data that is passed in
(including arrays) will be converted to a string with the normal JavaScript
`toString` method. For more information about sending binary data see
`Socket.write`
*/
// Re-use existing
/*JSON{
"type" : "method",
"class" : "httpCRq",
"name" : "end",
"generate" : "jswrap_net_socket_end",
"params" : [
["data","JsVar","A string containing data to send"]
]
}
Finish this HTTP request - optional data to append as an argument
See `Socket.write` for more information about the data argument
*/
// Re-use existing