Using Parser as a web server   [3.4.6]

/path/to/parser3 -p [host:]<port>

This command launches the web server built into the parser on the specified port, the root of the web-space will be the current directory (in which you are at the time of launch). In the web server mode, all requests are processed by the main method of the httpd class, which is added to the configuration auto.p and in which the logic of the web server is implemented - based on the address to which the user accessed (image, parser code, directory), web the server will either process the request itself (for example, return a file with an image), or transfer control to the file with the parser code to which the request is made. At startup, you can specify a specific IP address or name, then the parser will accept connections only on it. For example, when you run parser3 -p localhost:8000, connections will be accepted only on the local interface, closed from outside access. The built-in web server does not support encryption and keep-alive, in production it is more correct to use it in combination with nginx. Web server settings are located in the $cfg hash:

$cfg[
        $.parser[(\.html^$)]
- regular expression containing file extensions with parser code
        $.index[index.html] - name of the index file. When accessing the directory, it will be returned. To simplify the code there is a single index file, but it's easy to add another
#       $.autoindex(true) - if there is no index file, show listing of files in the directory
        $.404[$404] - handling 404 errors. You can call a method,
#       $.404[/404.html] - or you can transfer control to a specific file
#       $.fix-trailing-slash(true) - when requesting a directory without / at the end, issue a redirect by adding /
#       $.auth[ $.url[^^/\.?admin/] $.login[admin] $.password[change me] $.realm[site administration] ]
- require authorization for sections matching regular expression
        $.deny[(/\.ht[^^/]+|\.p|\.cfg)^$] - regular expression specifies which files are denied access, for example .htaccess and auto.p files
        $.403[Permission denied] - handling denied access. Can also be a method or a file
        $.memory(64000) - call the garbage collector if more than 64Mb of memory was allocated during processing of previously received requests
#       $.log[/access.log] - enable logging of incoming requests
]

The web server class is quite simple, only about a hundred lines, so you can either directly edit the logic of its operation, or use the built-in extension capabilities - you can place the httpd.p file in the root of the web-space, in which you can override the methods of the httpd class. In particular, you can override the
^config[] method (takes the default configuration as an argument and can change it before returning) and ^preprocess[] method (for handling redirects or replacing addresses).

In the configuration method, in
$HTTPD hash, you can set the operating mode of the web server and the timeout for processing connections:
·$.mode[sequental] - sequential processing of requests, by default. The parser works in one thread using one processor core.  
· $.mode[parallel] - parallel mode, not available under Windows. For each request, a separate process is created. If necessary, all cores are used, which gives maximum performance when processing code.  
· $.mode[threaded] - multi-threaded mode. A separate thread is created for each request. Due to the fact that all threads use the same garbage collector, performance is slightly lower than in parallel mode.  
· $.timeout(время)- sets the maximum timeout in seconds. If no data is received from the client within the specified time, the connection will be terminated. In multi-threaded mode, the specified timeout does not work, the actual timeout is determined by the operating system.  

Example

$HTTPD[
   $.mode[parallel]
   $.timeout(8)
]


Sets parallel mode and timeout to 8 seconds.



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 28.01.2021