scgi: Web Server HTTP SCGI and CGI in Racket
(require (planet neil/scgi:2:3)) | package: base |
1 Introduction
(require (planet neil/scgi:2)) (cgi #:startup (lambda () (printf "Startup for cgi-type ~S..." (cgi-type))) #:request (lambda () (display "Content-type: text/html\r\n\r\n") (display "<p>Hello, world!</p>") (printf "<p>Your IP address is: ~S</p>" (cgi-remote-addr))) #:shutdown (lambda () (printf "Shutdown down for cgi-type ~S..." (cgi-type))))
2 Interface
procedure
(make-cgi-variable-proc proc-name-sym name-bytes) → (-> string?) proc-name-sym : symbol? name-bytes : bytes?
(define cgi-remote-user (make-cgi-variable-proc 'cgi-remote-user #"REMOTE_USER"))
procedure
(cgi-document-root) → string? (cgi-http-cookie) → string? (cgi-http-host) → string? (cgi-http-referer) → string? (cgi-http-user-agent) → string? (cgi-https) → string? (cgi-path-info) → string? (cgi-path-translated) → string? (cgi-query-string) → string? (cgi-remote-addr) → string? (cgi-remote-host) → string? (cgi-remote-user) → string? (cgi-request-method) → string? (cgi-request-uri) → string? (cgi-script-name) → string? (cgi-server-name) → string? (cgi-server-port) → string?
3 Apache mod_scgi
3.1 Apache mod_scgi Configuration
LoadModule scgi_module /usr/lib/apache2/modules/mod_scgi.so |
SCGIMount /mypath 127.0.0.1:4000 |
SCGIServerTimeout 60 |
|
3.2 Apache mod_scgi Troubleshooting
Racket error “tcp-write: error writing (Broken pipe; errno=32)” or “tcp-write: error writing (Connection reset by peer; errno=104)” is likely due to the HTTP request having been dropped by the HTTP client (e.g., user stops a page load in their browser before page finishes loading) or by Apache hitting SCGIServerTimeout for the request. Note that buffered I/O means that you won’t necessarily get this error even if the request is aborted this way.
Apache error log entry “Premature end of script headers: PATH” followed by “(500)Unknown error 500: scgi: Unknown error 500: error reading response headers” can mean that SCGIServerTimeout was hit before any HTTP headers from the SCGI request handler were started or completed. Note that buffered I/O can mean that some of the Racket code of the handler wrote some text, but it was not yet flushed to the SCGI client.
Apache error log entry “(70007)The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed” followed by “(70007)The timeout specified has expired: scgi: The timeout specified has expired: ap_pass_brigade()” can mean that SCGIServerTimeout was hit after HTTP headers from the request handler had been received by the SCGI client.
Apache error log entry “(32)Broken pipe: scgi: Broken pipe: error sending request body” might be due to a request handler finished without consuming all of the HTTP POST data.
Apache error log entry “(103)Software caused connection abort: scgi: Software caused connection abort: ap_pass_brigade()” is another one that can be caused by the HTTP client dropping the connection before the handler finishes.
4 History
- PLaneT 2:3 —
2014-11-23 Removed file sample-1.rkt from package, to reduce package dependencies. (Suggested by Matt Gushee.)
- PLaneT 2:2 —
2013-04-19 Now accommodate SCGI variable names generated by Mellon authentication.
- PLaneT 2:1 —
2013-04-12 Now doesn’t barf on headers like proxy-nokeepalive. (Thanks to Erik for bug report and diagnosis.)
- PLaneT 2:0 —
2012-06-12 The three main procedures arguments to the cgi procedure now have keywords. What are now the #:startup and #:shutdown arguments are now also optional.
Converted to McFly.
Changed internal-only identifiers from having a % prefix to a %scgi: prefix, to help disambiguate when identifiers appear in error messages in large systems.
Documentation changes.
Improvement to the internal debugging macro.
- Version 0.6 —
PLaneT 1:5 — 2011-08-22 Small documentation fixes.
- Version 0.5 —
PLaneT 1:4 — 2011-05-16 For closer to backward compatibility with PLT Scheme 4.x, changed references to racket modules to scheme.
More documentation.
- Version 0.4 —
PLaneT 1:3 — 2011-05-16 Added #:reuse-scgi-port? argument to procedure cgi. Added several new CGI environment variable procedures.
Added make-cgi-variable-proc.
The cgi procedure now uses tcp-accept/enable-break rather than tcp-accept.
Added more log-debug.
Added documentation about mod_scgi configuration, troubleshooting,and the keyword arguments to the cgi procedure.
Removed documentation placeholders for end-cgi-request for now.
Various additional quality assurance testing has been done, and more is the works.
- Version 0.3 —
PLaneT 1:2 — 2010-11-14 Added cgi-http-user-agent.
- Version 0.2 —
PLaneT 1:1 — 2010-10-11 Documentation changes to reflect that it is successfully in use in a real system, and some work remains.
- Version 0.1 —
PLaneT 1:0 — 2010-05-25 Initial release. Preliminary.
5 Legal
Copyright 2010 – 2014 Neil Van Dyke. This program is Free Software; Software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See http://www.gnu.org/licenses/ for details. For other licenses and consulting, please contact the author.