/
jswrap_net.c
786 lines (696 loc) · 25 KB
/
jswrap_net.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2014 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 Net (socket) functions
* ----------------------------------------------------------------------------
*/
#include "jswrap_net.h"
#include "jsvariterator.h"
#include "jsinteractive.h"
#include "jsparse.h"
#include "socketserver.h"
#include "network.h"
/*JSON{
"type" : "idle",
"generate" : "jswrap_net_idle"
}*/
bool jswrap_net_idle() {
JsNetwork net;
if (!networkGetFromVar(&net)) return false;
net.idle(&net);
bool b = socketIdle(&net);
networkFree(&net);
return b;
}
/*JSON{
"type" : "init",
"generate" : "jswrap_net_init"
}*/
void jswrap_net_init() {
socketInit();
}
/*JSON{
"type" : "kill",
"generate" : "jswrap_net_kill"
}*/
void jswrap_net_kill() {
JsNetwork net;
if (networkWasCreated()) {
if (!networkGetFromVar(&net)) return;
socketKill(&net);
networkFree(&net);
}
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "class",
"class" : "url"
}
This class helps to convert URLs into Objects of information ready for
http.request/get
*/
/*JSON{
"type" : "staticmethod",
"class" : "url",
"name" : "parse",
"generate" : "jswrap_url_parse",
"params" : [
["urlStr","JsVar","A URL to be parsed"],
["parseQuery","bool","Whether to parse the query string into an object not (default = false)"]
],
"return" : ["JsVar","An object containing options for ```http.request``` or ```http.get```. Contains `method`, `host`, `path`, `pathname`, `search`, `port` and `query`"]
}
A utility function to split a URL into parts
This is useful in web servers for instance when handling a request.
For instance `url.parse("/a?b=c&d=e",true)` returns
`{"method":"GET","host":"","path":"/a?b=c&d=e","pathname":"/a","search":"?b=c&d=e","port":80,"query":{"b":"c","d":"e"}}`
*/
JsVar *jswrap_url_parse(JsVar *url, bool parseQuery) {
if (!jsvIsString(url)) return 0;
JsVar *obj = jsvNewObject();
if (!obj) return 0; // out of memory
// scan string to try and pick stuff out
JsvStringIterator it;
jsvStringIteratorNew(&it, url, 0);
int slashes = 0;
int colons = 0;
int addrStart = -1;
int portStart = -1;
int pathStart = -1;
int searchStart = -1;
int charIdx = 0;
int portNumber = 0;
while (jsvStringIteratorHasChar(&it)) {
char ch = jsvStringIteratorGetCharAndNext(&it);
if (ch == '/') {
slashes++;
if (pathStart<0) pathStart = charIdx;
if (colons==1 && slashes==2 && addrStart<0) {
addrStart = charIdx;
pathStart = -1;
searchStart = -1;
}
}
if (ch == ':') {
colons++;
if (addrStart>=0 && pathStart<0)
portStart = charIdx;
}
if (portStart>=0 && charIdx>portStart && pathStart<0 && ch >= '0' && ch <= '9') {
portNumber = portNumber*10 + (ch-'0');
}
if (ch == '?' && pathStart>=0) {
searchStart = charIdx;
}
charIdx++;
}
jsvStringIteratorFree(&it);
// try and sort stuff out
if (pathStart<0) pathStart = charIdx;
if (pathStart<0) pathStart = charIdx;
int addrEnd = (portStart>=0) ? portStart : pathStart;
// pull out details
if (addrStart>0)
jsvObjectSetChildAndUnLock(obj, "protocol", jsvNewFromStringVar(url, 0, (size_t)addrStart-1));
jsvObjectSetChildAndUnLock(obj, "method", jsvNewFromString("GET"));
jsvObjectSetChildAndUnLock(obj, "host", jsvNewFromStringVar(url, (size_t)(addrStart+1), (size_t)(addrEnd-(addrStart+1))));
JsVar *v;
v = jsvNewFromStringVar(url, (size_t)pathStart, JSVAPPENDSTRINGVAR_MAXLENGTH);
if (jsvGetStringLength(v)==0) jsvAppendString(v, "/");
jsvObjectSetChildAndUnLock(obj, "path", v);
v = jsvNewFromStringVar(url, (size_t)pathStart, (size_t)((searchStart>=0)?(searchStart-pathStart):JSVAPPENDSTRINGVAR_MAXLENGTH));
if (jsvGetStringLength(v)==0) jsvAppendString(v, "/");
jsvObjectSetChildAndUnLock(obj, "pathname", v);
jsvObjectSetChildAndUnLock(obj, "search", (searchStart>=0)?jsvNewFromStringVar(url, (size_t)searchStart, JSVAPPENDSTRINGVAR_MAXLENGTH):jsvNewNull());
jsvObjectSetChildAndUnLock(obj, "port", (portNumber<=0 || portNumber>65535) ? jsvNewWithFlags(JSV_NULL) : jsvNewFromInteger(portNumber));
JsVar *query = (searchStart>=0)?jsvNewFromStringVar(url, (size_t)(searchStart+1), JSVAPPENDSTRINGVAR_MAXLENGTH):jsvNewNull();
if (parseQuery && !jsvIsNull(query)) {
JsVar *queryStr = query;
jsvStringIteratorNew(&it, query, 0);
query = jsvNewObject();
JsVar *key = jsvNewFromEmptyString();
JsVar *val = jsvNewFromEmptyString();
bool hadEquals = false;
while (jsvStringIteratorHasChar(&it)) {
char ch = jsvStringIteratorGetCharAndNext(&it);
if (ch=='&') {
if (jsvGetStringLength(key)>0 || jsvGetStringLength(val)>0) {
key = jsvAsArrayIndexAndUnLock(key); // make sure "0" gets made into 0
key = jsvMakeIntoVariableName(key, val);
jsvAddName(query, key);
jsvUnLock2(key, val);
key = jsvNewFromEmptyString();
val = jsvNewFromEmptyString();
hadEquals = false;
}
} else if (!hadEquals && ch=='=') {
hadEquals = true;
} else {
// decode percent escape chars
if (ch=='%') {
char hi = jsvStringIteratorGetCharAndNext(&it);
char lo = jsvStringIteratorGetCharAndNext(&it);
ch = (char)hexToByte(hi,lo);
}
if (hadEquals) jsvAppendCharacter(val, ch);
else jsvAppendCharacter(key, ch);
}
charIdx++;
}
jsvStringIteratorFree(&it);
jsvUnLock(queryStr);
if (jsvGetStringLength(key)>0 || jsvGetStringLength(val)>0) {
key = jsvAsArrayIndexAndUnLock(key); // make sure "0" gets made into 0
key = jsvMakeIntoVariableName(key, val);
jsvAddName(query, key);
}
jsvUnLock2(key, val);
}
jsvObjectSetChildAndUnLock(obj, "query", query);
return obj;
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "library",
"class" : "net"
}
This library allows you to create TCPIP servers and clients
In order to use this, you will need an extra module to get network connectivity.
This is designed to be a cut-down version of the [node.js
library](http://nodejs.org/api/net.html). Please see the [Internet](/Internet)
page for more information on how to use it.
*/
/*JSON{
"type" : "class",
"library" : "net",
"class" : "Server"
}
The socket server created by `require('net').createServer`
*/
/*JSON{
"type" : "class",
"library" : "net",
"class" : "Socket"
}
An actual socket connection - allowing transmit/receive of TCP data
*/
/*JSON{
"type" : "event",
"class" : "Socket",
"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" : "Socket",
"name" : "close",
"params" : [
["had_error","JsVar","A boolean indicating whether the connection had an error (use an error event handler to get error details)."]
]
}
Called when the connection closes.
*/
/*JSON{
"type" : "event",
"class" : "Socket",
"name" : "error",
"params" : [
["details","JsVar","An error object with an error code (a negative integer) and a message."]
]
}
There was an error on this socket and it is closing (or wasn't opened in the
first place). If a "connected" event was issued on this socket then the error
event is always followed by a close event. The error codes are:
* -1: socket closed (this is not really an error and will not cause an error
callback)
* -2: out of memory (typically while allocating a buffer to hold data)
* -3: timeout
* -4: no route
* -5: busy
* -6: not found (DNS resolution)
* -7: max sockets (... exceeded)
* -8: unsent data (some data could not be sent)
* -9: connection reset (or refused)
* -10: unknown error
* -11: no connection
* -12: bad argument
* -13: SSL handshake failed
* -14: invalid SSL data
*/
/*JSON{
"type" : "method",
"class" : "Socket",
"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" : "Socket",
"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" : "Socket",
"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" : "event",
"class" : "Socket",
"name" : "drain"
}
An event that is fired when the buffer is empty and it can accept more data to
send.
*/
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "staticmethod",
"class" : "net",
"name" : "createServer",
"generate" : "jswrap_net_createServer",
"params" : [
["callback","JsVar","A `function(connection)` that will be called when a connection is made"]
],
"return" : ["JsVar","Returns a new Server Object"],
"return_object" : "Server"
}
Create a Server
When a request to the server is made, the callback is called. In the callback
you can use the methods on the connection to send data. You can also add
`connection.on('data',function() { ... })` to listen for received data
*/
JsVar *jswrap_net_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_NORMAL, callback);
}
/*JSON{
"type" : "staticmethod",
"class" : "net",
"name" : "connect",
"generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL)",
"params" : [
["options","JsVar","An object containing host,port fields"],
["callback","JsVar","A `function(sckt)` that will be called with the socket when a connection is made. You can then call `sckt.write(...)` to send data, and `sckt.on('data', function(data) { ... })` and `sckt.on('close', function() { ... })` to deal with the response."]
],
"return" : ["JsVar","Returns a new net.Socket object"],
"return_object" : "Socket"
}
Create a TCP socket connection
*/
JsVar *jswrap_net_connect(JsVar *options, JsVar *callback, SocketType socketType) {
bool unlockOptions = false;
if (jsvIsString(options)) {
options = jswrap_url_parse(options, false);
unlockOptions = true;
}
if (!jsvIsObject(options)) {
jsError("Expecting Options to be an Object but it was %t", options);
return 0;
}
#ifdef USE_TLS
if ((socketType&ST_TYPE_MASK) == ST_HTTP) {
JsVar *protocol = jsvObjectGetChildIfExists(options, "protocol");
if (protocol && jsvIsStringEqual(protocol, "https:")) {
socketType |= ST_TLS;
}
jsvUnLock(protocol);
}
#endif
// Make sure we have a function as callback, or nothing (which is OK too)
if (!jsvIsUndefined(callback) && !jsvIsFunction(callback)) {
jsError("Expecting Callback Function but got %t", callback);
return 0;
}
if ((socketType&ST_TYPE_MASK) == ST_UDP) {
JsNetwork net;
if (networkGetFromVar(&net)) {
int recvBufferSize = jsvObjectGetIntegerChild(options, "recvBufferSize");
if (recvBufferSize > net.data.recvBufferSize) {
net.data.recvBufferSize = recvBufferSize;
networkSet(&net);
}
networkFree(&net);
}
}
JsVar *rq = clientRequestNew(socketType, options, callback);
if (unlockOptions) jsvUnLock(options);
if ((socketType&ST_TYPE_MASK) == ST_NORMAL) {
JsNetwork net;
if (networkGetFromVarIfOnline(&net)) {
clientRequestConnect(&net, rq);
networkFree(&net);
}
}
return rq;
}
/*JSON{
"type" : "library",
"class" : "dgram"
}
This library allows you to create UDP/DATAGRAM servers and clients
In order to use this, you will need an extra module to get network connectivity.
This is designed to be a cut-down version of the [node.js
library](http://nodejs.org/api/dgram.html). Please see the [Internet](/Internet)
page for more information on how to use it.
*/
/*JSON{
"type" : "staticmethod",
"class" : "dgram",
"name" : "createSocket",
"generate_full" : "jswrap_dgram_createSocket(type, callback)",
"params" : [
["type","JsVar","Socket type to create e.g. 'udp4'. Or options object { type: 'udp4', reuseAddr: true, recvBufferSize: 1024 }"],
["callback","JsVar","A `function(sckt)` that will be called with the socket when a connection is made. You can then call `sckt.send(...)` to send data, and `sckt.on('message', function(data) { ... })` and `sckt.on('close', function() { ... })` to deal with the response."]
],
"return" : ["JsVar","Returns a new dgram.Socket object"],
"return_object" : "dgramSocket"
}
Create a UDP socket
*/
JsVar *jswrap_dgram_createSocket(JsVar *type, JsVar *callback) {
NOT_USED(type);
JsVar *options = jsvIsObject(type) ? type : jsvNewObject();
JsVar *connection = jswrap_net_connect(options, callback, ST_UDP);
if (options != type) jsvUnLock(options);
return connection;
}
/*JSON{
"type" : "class",
"library" : "dgram",
"class" : "dgramSocket"
}
An actual socket connection - allowing transmit/receive of TCP data
*/
/*JSON{
"type" : "method",
"class" : "dgramSocket",
"name" : "send",
"generate" : "jswrap_dgram_socket_send",
"params" : [
["buffer","JsVar","A string containing message to send"],
["offset","JsVar","Offset in the passed string where the message starts [optional]"],
["length","JsVar","Number of bytes in the message [optional]"],
["args","JsVarArray","Destination port number, Destination IP address string"]
]
}*/
// There are futher arguments within the 'args' JsVarArray:
// ["port","JsVar","Destination port number to send the message to"],
// ["address","JsVar","Destination hostname or IP address string"]
void jswrap_dgram_socket_send(JsVar *parent, JsVar *buffer, JsVar *offset, JsVar *length, JsVar *args) {
assert(jsvIsObject(parent));
assert(jsvIsString(buffer));
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return;
JsVar *msg;
JsVar *address;
JsVar *port = jsvGetArrayItem(args, 0);
if (jsvIsNumeric(port)) {
int from = jsvGetInteger(offset);
int count = jsvGetInteger(length);
msg = jsvNewFromEmptyString();
if (!msg) {
networkFree(&net);
return; // out of memory
}
jsvAppendStringVar(msg, buffer, (size_t)from, (size_t)count);
address = jsvGetArrayItem(args, 1);
} else {
jsvUnLock(port);
msg = buffer;
port = offset;
address = length;
assert(jsvIsNumeric(port));
}
clientRequestWrite(&net, parent, msg, address, (unsigned short)jsvGetInteger(port));
if (msg != buffer) {
jsvUnLock3(msg, port, address);
}
networkFree(&net);
}
/*JSON{
"type" : "event",
"class" : "dgramSocket",
"name" : "message",
"params" : [
["msg","JsVar","A string containing the received message"],
["rinfo","JsVar","Sender address,port containing information"]
]
}
The 'message' event is called when a datagram message is received. If a handler
is defined with `X.on('message', function(msg) { ... })` then it will be called`
*/
/*JSON{
"type" : "method",
"class" : "dgramSocket",
"name" : "bind",
"generate" : "jswrap_dgramSocket_bind",
"params" : [
["port","int32","The port to bind at"],
["callback","JsVar","A function(res) that will be called when the socket is bound. You can then call `res.on('message', function(message, info) { ... })` and `res.on('close', function() { ... })` to deal with the response."]
],
"return" : ["JsVar","The dgramSocket instance that 'bind' was called on"]
}
*/
JsVar *jswrap_dgramSocket_bind(JsVar *parent, unsigned short port, JsVar *callback) {
parent = jsvLockAgain(parent); // we're returning the parent, so need to re-lock it
jsvObjectSetChild(parent, DGRAM_ON_BIND_NAME, callback);
jsvUnLock(jswrap_net_server_listen(parent, port, ST_UDP)); // create bound socket
jsiQueueObjectCallbacks(parent, DGRAM_ON_BIND_NAME, &parent, 1);
return parent;
}
/*JSON{
"type" : "method",
"class" : "dgramSocket",
"name" : "close",
"generate" : "jswrap_dgram_close"
}
Close the socket
*/
void jswrap_dgram_close(JsVar *parent) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return;
clientRequestEnd(&net, parent);
networkFree(&net);
}
/*JSON{
"type" : "method",
"class" : "dgramSocket",
"name" : "addMembership",
"generate" : "jswrap_dgram_addMembership",
"params" : [
["group","JsVar","A string containing the group ip to join"],
["ip","JsVar","A string containing the ip to join with"]
]
}
*/
void jswrap_dgram_addMembership(JsVar *parent, JsVar *group, JsVar *ip) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return;
serverAddMembership(&net, parent, group, ip);
}
/*JSON{
"type" : "event",
"class" : "dgramSocket",
"name" : "close",
"params" : [
["had_error","JsVar","A boolean indicating whether the connection had an error (use an error event handler to get error details)."]
]
}
Called when the connection closes.
*/
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "library",
"class" : "tls",
"ifdef" : "USE_TLS"
}
This library allows you to create TCPIP servers and clients using TLS encryption
In order to use this, you will need an extra module to get network connectivity.
This is designed to be a cut-down version of the [node.js
library](http://nodejs.org/api/tls.html). Please see the [Internet](/Internet)
page for more information on how to use it.
*/
/*JSON{
"type" : "staticmethod",
"class" : "tls",
"name" : "connect",
"generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL | ST_TLS)",
"params" : [
["options","JsVar","An object containing host,port 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 net.Socket object"],
"return_object" : "Socket",
"ifdef" : "USE_TLS"
}
Create a socket connection using TLS
Options can have `ca`, `key` and `cert` fields, which should be the decoded
content of the certificate.
```
var options = url.parse("localhost:1234");
options.key = atob("MIIJKQ ... OZs08C");
options.cert = atob("MIIFi ... Uf93rN+");
options.ca = atob("MIIFgDCC ... GosQML4sc=");
require("tls").connect(options, ... );
```
If you have the certificates as `.pem` files, you need to load these files, take
the information between the lines beginning with `----`, remove the newlines
from it so you have raw base64, and then feed it into `atob` as above.
You can also:
* Just specify the filename (<=100 characters) and it will be loaded and parsed
if you have an SD card connected. For instance `options.key = "key.pem";`
* Specify a function, which will be called to retrieve the data. For instance
`options.key = function() { eeprom.load_my_info(); };
For more information about generating and using certificates, see:
https://engineering.circle.com/https-authorized-certs-with-node-js/
(You'll need to use 2048 bit certificates as opposed to 4096 bit shown above)
*/
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "method",
"class" : "Server",
"name" : "listen",
"generate_full" : "jswrap_net_server_listen(parent, port, ST_NORMAL)",
"params" : [
["port","int32","The port to listen on"]
],
"return" : ["JsVar","The HTTP server instance that 'listen' was called on"]
}
Start listening for new connections on the given port
*/
JsVar *jswrap_net_server_listen(JsVar *parent, int port, SocketType socketType) {
parent = jsvLockAgain(parent); // we're returning the parent, so need to re-lock it
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return parent;
serverListen(&net, parent, (unsigned short)port, socketType);
networkFree(&net);
return parent;
}
/*JSON{
"type" : "method",
"class" : "Server",
"name" : "close",
"generate" : "jswrap_net_server_close"
}
Stop listening for new connections
*/
void jswrap_net_server_close(JsVar *parent) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return;
serverClose(&net, parent);
networkFree(&net);
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
/*JSON{
"type" : "method",
"class" : "Socket",
"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.
If you wish to send binary data then you need to convert that data directly to a
String. This can be done with `String.fromCharCode`, however it's often easier
and faster to use the Espruino-specific `E.toString`, which will read its
arguments as an array of bytes and convert that to a String:
```
socket.write(E.toString([0,1,2,3,4,5]));
```
If you need to send something other than bytes, you can use 'Typed Arrays', or
even `DataView`:
```
var d = new DataView(new ArrayBuffer(8)); // 8 byte array buffer
d.setFloat32(0, 765.3532564); // write float at bytes 0-3
d.setInt8(4, 42); // write int8 at byte 4
socket.write(E.toString(d.buffer))
```
*/
bool jswrap_net_socket_write(JsVar *parent, JsVar *data) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return false;
clientRequestWrite(&net, parent, data, NULL, 0);
networkFree(&net);
return false;
}
/*JSON{
"type" : "method",
"class" : "Socket",
"name" : "end",
"generate" : "jswrap_net_socket_end",
"params" : [
["data","JsVar","A string containing data to send"]
]
}
Close this socket - optional data to append as an argument.
See `Socket.write` for more information about the data argument
*/
void jswrap_net_socket_end(JsVar *parent, JsVar *data) {
JsNetwork net;
if (!networkGetFromVarIfOnline(&net)) return;
if (!jsvIsUndefined(data)) jswrap_net_socket_write(parent, data);
clientRequestEnd(&net, parent);
networkFree(&net);
}