@@ -21,10 +21,10 @@ function must(result) {
21
21
return /** @type {T } */ ( result )
22
22
}
23
23
/**@param {os.FileDescriptor } fd */
24
- function * recvLines ( fd ) {
24
+ async function * recvLines ( fd ) {
25
25
const chunk = new Uint8Array ( 1 ) ;
26
26
let line = '' ;
27
- while ( os . read ( fd , chunk . buffer , 0 , chunk . byteLength ) > 0 ) {
27
+ while ( await os . recv ( fd , chunk . buffer ) > 0 ) {
28
28
const char = String . fromCharCode ( ...chunk ) ;
29
29
if ( char == '\n' ) {
30
30
yield line ;
@@ -36,7 +36,7 @@ function* recvLines(fd) {
36
36
/** @param {os.FileDescriptor } fd @param {string[] } lines */
37
37
function sendLines ( fd , lines ) {
38
38
const buf = Uint8Array . from ( lines . join ( '\r\n' ) , c => c . charCodeAt ( 0 ) ) ;
39
- os . write ( fd , buf . buffer , 0 , buf . byteLength ) ;
39
+ os . send ( fd , buf . buffer ) ;
40
40
}
41
41
//USAGE: qjs http_server.js [PORT=8080 [HOST=localhost]]
42
42
const [ port = "8080" , host = "localhost" ] = scriptArgs . slice ( 1 ) ;
@@ -47,19 +47,20 @@ const sock_srv = must(os.socket(os.AF_INET, os.SOCK_STREAM));
47
47
must ( os . setsockopt ( sock_srv , os . SO_REUSEADDR , new Uint32Array ( [ 1 ] ) . buffer ) ) ;
48
48
must ( os . bind ( sock_srv , ai [ 0 ] ) ) ;
49
49
must ( os . listen ( sock_srv ) ) ;
50
-
51
- console . log ( `Listening on http://${ ai [ 0 ] . addr } :${ ai [ 0 ] . port } ...` ) ;
52
-
50
+ //os.signal(os.SIGINT, ()=>os.close(sock_srv)); // don't work
51
+ console . log ( `Listening on http://${ host } :${ port } (${ ai [ 0 ] . addr } :${ ai [ 0 ] . port } ) ...` ) ;
52
+ const openCmd = { linux : "xdg-open" , darwin : "open" , win32 : "start" } [ os . platform ] ;
53
+ if ( openCmd ) os . exec ( [ openCmd , `http://${ host } :${ port } ` ] ) ;
53
54
while ( true ) { // TODO: break on SIG*
54
- const [ sock_cli ] = os . accept ( sock_srv ) ;
55
+ const [ sock_cli ] = await os . accept ( sock_srv ) ;
55
56
56
57
const lines = recvLines ( sock_cli ) ;
57
- const [ method , path , http_ver ] = ( lines . next ( ) . value || '' ) . split ( ' ' ) ;
58
+ const [ method , path , http_ver ] = ( ( await lines . next ( ) ) . value || '' ) . split ( ' ' ) ;
58
59
let safe_path = '.' + path . replaceAll ( / \. + / g, '.' ) ; // may += index.html later
59
60
console . log ( method , safe_path , http_ver ) ;
60
61
61
62
const headers = new Map ( )
62
- for ( const line of lines ) {
63
+ for await ( const line of lines ) {
63
64
const header = line . trimEnd ( ) ;
64
65
if ( ! header ) break ;
65
66
const sepIdx = header . indexOf ( ': ' ) ;
@@ -86,11 +87,9 @@ while (true) { // TODO: break on SIG*
86
87
const fd = must ( os . open ( safe_path ) ) ;
87
88
const fbuf = new Uint8Array ( 4096 ) ;
88
89
for ( let got = 0 ; ( got = os . read ( fd , fbuf . buffer , 0 , fbuf . byteLength ) ) > 0 ; ) {
89
- os . write ( sock_cli , fbuf . buffer , 0 , got ) ;
90
+ os . send ( sock_cli , fbuf . buffer , got ) ;
90
91
}
91
92
}
92
93
93
94
os . close ( sock_cli ) ;
94
95
}
95
-
96
- os . close ( sock_srv ) ;
0 commit comments