Check-in [9c4ec6ad9d]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:use textual port for scgi response
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9c4ec6ad9dc958f381fabbdb4439219713ec652c
User & Date: aldo 2016-12-11 16:11:59
Context
2016-12-12
14:03
fixed bug in scgi server loop, the child process would not exit if an exception was raised check-in: 7c452271be user: aldo tags: trunk
2016-12-11
16:11
use textual port for scgi response check-in: 9c4ec6ad9d user: aldo tags: trunk
15:18
added more useful scgi example check-in: 4c5a36a864 user: aldo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to scgi.sls.

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
...
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
		(append (bytevector->u8-list (string->utf8 name)) '(0)
			(bytevector->u8-list (string->utf8 value)) '(0)
			acc)))
	    '() l )))

  (define scgi-request-handler
    (make-parameter
     (lambda (sock headers content)
       (printf "scgi: headers: ~a~n" headers)
       (printf "scgi: contents: ~a~n" content)

       (put-bytevector sock (string->utf8 "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><center><h1><big>WELCOME TO THUNDERCHEZ!</big></h1></center></body></html>")))))
  
  (define (handle-scgi-connection sock)
    (define h (read-headers sock))
    (assert (string=? "1" (cdr (assq 'SCGI h))))
    (let* ([len (string->number (cdr (assq 'CONTENT_LENGTH h)))]
	   [content (get-bytevector-n sock len)])
      (assert (= (bytevector-length content) len))




      ((scgi-request-handler) sock h content)))


  (define (run-scgi addr port)
    (define nchildren 0)
    (define max-children 10)
    (define waitpid (foreign-procedure "waitpid" (int void* int) int))
    (call-with-port
     (socket 'inet 'stream '() 0)
     (lambda (sock)	
       (bind/inet sock addr port)
       (listen sock 1000)
       (do ()
	   (#f)
	 (printf "scgi: waiting for connection...~n")
	 (let ([cli #f])
	   (call-with-port
	    (accept sock)
	    (lambda (clifd)
	      (printf "scgi: accepted connection~n")
	      (if (> nchildren max-children)
		  (sleep (make-time 'time-duration 0 1)))
	      (printf "scgi: forking..~n")
	      (let ([pid (fork)])
		(if (= pid 0)			
		    (guard (e [else (display "scgi: handler error: ")
				    (display-condition e)
				    (newline)])
			   (handle-scgi-connection clifd)
			   (exit))
		    (set! nchildren (+ 1 nchildren)))))))
	 (do ()
	     ((not (> (waitpid 0 0 (wait-flag 'nohang)) 0)))
	   (set! nchildren (- nchildren 1)))))))
     
     );;library scgi


................................................................................

(import (chezscheme)
	(scgi)
	(sxml)
	(sxml to-html))
(parameterize
 ([scgi-request-handler
   (lambda (sock headers content)
     (let ([xml (with-output-to-string
		  (lambda ()
		    (SXML->HTML '(html (h1 "WELCOME TO THE WEB!")))))])
       (put-bytevector sock (string->utf8 "Status: 200 OK\r\n"))
       (put-bytevector sock (string->utf8 "Content-Type: text/html\r\n"))
       (put-bytevector sock (string->utf8 "\r\n"))
       (put-bytevector sock (string->utf8 xml))))])


       
 (run-scgi "localhost" 8088))
		


;CLIENT EXAMPLE:
(import (netstring) 
	(socket)
	(scgi))

(define sock (socket 'inet 'stream '() 0))







|


>
|







>
>
>
>
|
>













<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







|
|
<
<
|
|
<
<
>
>
|

<
<







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
...
115
116
117
118
119
120
121
122
123


124
125


126
127
128
129


130
131
132
133
134
135
136
		(append (bytevector->u8-list (string->utf8 name)) '(0)
			(bytevector->u8-list (string->utf8 value)) '(0)
			acc)))
	    '() l )))

  (define scgi-request-handler
    (make-parameter
     (lambda (response-port headers content)
       (printf "scgi: headers: ~a~n" headers)
       (printf "scgi: contents: ~a~n" content)
       
       (display "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><center><h1><big>WELCOME TO THUNDERCHEZ!</big></h1></center></body></html>" response-port))))
  
  (define (handle-scgi-connection sock)
    (define h (read-headers sock))
    (assert (string=? "1" (cdr (assq 'SCGI h))))
    (let* ([len (string->number (cdr (assq 'CONTENT_LENGTH h)))]
	   [content (get-bytevector-n sock len)])
      (assert (= (bytevector-length content) len))
      (let ([port (open-fd-output-port
		   (port-file-descriptor sock)
		   'block
		   (make-transcoder (utf-8-codec) 'none))])
	((scgi-request-handler) port h content)
	(flush-output-port port))))

  (define (run-scgi addr port)
    (define nchildren 0)
    (define max-children 10)
    (define waitpid (foreign-procedure "waitpid" (int void* int) int))
    (call-with-port
     (socket 'inet 'stream '() 0)
     (lambda (sock)	
       (bind/inet sock addr port)
       (listen sock 1000)
       (do ()
	   (#f)
	 (printf "scgi: waiting for connection...~n")

	 (call-with-port
	  (accept sock)
	  (lambda (clifd)
	    (printf "scgi: accepted connection~n")
	    (if (> nchildren max-children)
		(sleep (make-time 'time-duration 0 1)))
	    (printf "scgi: forking..~n")
	    (let ([pid (fork)])
	      (if (= pid 0)
		  (guard (e [else (display "scgi: handler error: ")
				  (display-condition e)
				  (newline)])			   
			 (handle-scgi-connection clifd)
			 (exit))
		  (set! nchildren (+ 1 nchildren))))))
	 (do ()
	     ((not (> (waitpid 0 0 (wait-flag 'nohang)) 0)))
	   (set! nchildren (- nchildren 1)))))))
     
     );;library scgi


................................................................................

(import (chezscheme)
	(scgi)
	(sxml)
	(sxml to-html))
(parameterize
 ([scgi-request-handler
   (lambda (response-port headers content)
     (parameterize ([current-output-port response-port])


		   (display "Status: 200 OK\r\n")
		   (display "Content-Type: text/html\r\n")


		   (display "\r\n")
		   (SXML->HTML '(html (h1 "WELCOME TO THE WEB!")))))])
 
 (run-scgi "localhost" 8088))



;CLIENT EXAMPLE:
(import (netstring) 
	(socket)
	(scgi))

(define sock (socket 'inet 'stream '() 0))