WebGate

Check-in [8e0d0801c6]
Login

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

Overview
Comment:Improved listener configuration, HTTP_ prefix for soup header variables
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8e0d0801c6946ff44a4bef52f301d7da3384288c
User & Date: murphy 2015-05-04 12:19:48
Context
2015-05-04
14:56
Added BZip2 compression function wrappers check-in: b520ce0466 user: murphy tags: trunk
12:19
Improved listener configuration, HTTP_ prefix for soup header variables check-in: 8e0d0801c6 user: murphy tags: trunk
11:11
Support for REMOTE_ADDR and REMOTE_PORT in the soup adapter check-in: 362eecda51 user: murphy tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to webgate-soup.scm.

27
28
29
30
31
32
33
34









35




36
37
38
39
40
41
42
...
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
  "#include <glib.h>\n"
  "#include <libsoup/soup.h>\n")

(foreign-code
  "g_type_init();")

(define soup-server-new
  (foreign-lambda* (c-pointer "SoupServer") ((unsigned-integer port))









    "C_return(soup_server_new(SOUP_SERVER_PORT, port, SOUP_SERVER_RAW_PATHS, TRUE, NULL));"))





(define-external (webgate_soup_callback ((c-pointer "SoupServer") server)
					((c-pointer "SoupMessage") message)
					((const c-string) path)
					((c-pointer "GHashTable") query)
					((c-pointer "SoupClientContext") client)
					(c-pointer root)) void
................................................................................
    (message-headers rsp))
  (cond
   ((message-body rsp)
    => (cut soup-response-body-add! message <>))))

;; soup server loop

(define (soup-main-loop handle-request port)
  (let ((server (soup-server-new port)))


    (soup-server-add-handler
     server #f
     (lambda (server client message path)
       (handle-request
        (lambda (name)
          (cond


           ((string=? name "REQUEST_METHOD")
            (soup-message-method message))
           ((string=? name "PATH_INFO")
            path)
           ((string=? name "QUERY_STRING")
            (soup-message-query message))
	   ((or (string=? name "REMOTE_ADDR") (string=? name "REMOTE_HOST"))
	    (soup-client-address client))
	   ((string=? name "REMOTE_PORT")
	    (cond ((soup-client-port client) => number->string) (else #f)))
           (else
            (soup-request-header-ref message (string-translate name #\_ #\-)))))
        (open-input-string (soup-request-body message))
        (cut soup-response-set! message <>))))
    (soup-server-run server)))







|
>
>
>
>
>
>
>
>
>
|
>
>
>
>







 







|
|
>
>






>
>










|
|



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
...
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
  "#include <glib.h>\n"
  "#include <libsoup/soup.h>\n")

(foreign-code
  "g_type_init();")

(define soup-server-new
  (foreign-lambda* (c-pointer "SoupServer") ((c-string host) (unsigned-integer port))
    "SoupAddress *address;\n"
    "if (host) {\n"
    "  address = soup_address_new(host, port);\n"
    "  if (soup_address_resolve_sync(address, NULL) != SOUP_STATUS_OK) C_return(NULL);\n"
    "} else {\n"
    "  address = soup_address_new_any(SOUP_ADDRESS_FAMILY_IPV4, port);"
    "}\n"
    "SoupServer *server = soup_server_new("
    "  SOUP_SERVER_INTERFACE, address,"
    "  SOUP_SERVER_RAW_PATHS, TRUE,"
    "  NULL"
    ");\n"
    "g_object_unref(address);\n"
    "C_return(server);"))

(define-external (webgate_soup_callback ((c-pointer "SoupServer") server)
					((c-pointer "SoupMessage") message)
					((const c-string) path)
					((c-pointer "GHashTable") query)
					((c-pointer "SoupClientContext") client)
					(c-pointer root)) void
................................................................................
    (message-headers rsp))
  (cond
   ((message-body rsp)
    => (cut soup-response-body-add! message <>))))

;; soup server loop

(define (soup-main-loop handle-request port #!optional host)
  (let ((server (soup-server-new host port)))
    (unless server
      (error 'soup-main-loop "Failed to create server" host port))
    (soup-server-add-handler
     server #f
     (lambda (server client message path)
       (handle-request
        (lambda (name)
          (cond
           ((string-prefix? "HTTP_" name)
            (soup-request-header-ref message (string-translate (substring/shared name 5) #\_ #\-)))
           ((string=? name "REQUEST_METHOD")
            (soup-message-method message))
           ((string=? name "PATH_INFO")
            path)
           ((string=? name "QUERY_STRING")
            (soup-message-query message))
	   ((or (string=? name "REMOTE_ADDR") (string=? name "REMOTE_HOST"))
	    (soup-client-address client))
	   ((string=? name "REMOTE_PORT")
	    (cond ((soup-client-port client) => number->string) (else #f)))
	   (else
	    #f)))
        (open-input-string (soup-request-body message))
        (cut soup-response-set! message <>))))
    (soup-server-run server)))

Changes to webgate.scm.

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
...
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

(cond-expand
  (enable-soup
   (module webgate-soup
     (soup-main-loop)
     (import
      scheme chicken foreign
      srfi-1
      data-structures webgate-core)
     (include
      "webgate-soup.scm")))
  (else))

(module webgate
  (webgate-main)
  (import
   scheme chicken
   srfi-13 webgate-cgi webgate-scgi tcp
   (only webgate-core
	 handle-request)
   (only webgate-suspend
	 current-suspension-key))
  (cond-expand
    (enable-soup
     (import webgate-soup))
................................................................................
	 response-status response-status-message
	 define-resource resource-uri)
   (only webgate-suspend
	 send/suspend))

(define (webgate-main #!optional (arguments (command-line-arguments)))
  (apply
   (lambda (#!key (port #f) (backlog 4) (host "localhost") (suspension-key #f))
     (cond
      (suspension-key => current-suspension-key))








     (cond
      ((and port (equal? host "http:*"))





       (cond-expand
        (enable-soup
         (soup-main-loop handle-request (string->number port)))


        (else
         (error 'webgate-main "HTTP support not enabled"))))
      (port



       (let ((ear (tcp-listen (string->number port) backlog host)))
         (dynamic-wind
	     void
	     (cut scgi-main-loop handle-request ear)
	     (cut tcp-close ear))))




      (else




       (cgi-main-loop handle-request))))
   (map
    (lambda (arg)
      (if (string-prefix? "-" arg)
	  (string->keyword (substring/shared arg 1))
	  arg))
    arguments)))

)







|









|







 







|


>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
|
<
<
>
>
|
<
<
>
>
>
|
|
|
|
|
>
>
>
>
|
>
>
>
>
|








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
...
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

(cond-expand
  (enable-soup
   (module webgate-soup
     (soup-main-loop)
     (import
      scheme chicken foreign
      srfi-1 srfi-13
      data-structures webgate-core)
     (include
      "webgate-soup.scm")))
  (else))

(module webgate
  (webgate-main)
  (import
   scheme chicken
   srfi-13 irregex webgate-cgi webgate-scgi tcp
   (only webgate-core
	 handle-request)
   (only webgate-suspend
	 current-suspension-key))
  (cond-expand
    (enable-soup
     (import webgate-soup))
................................................................................
	 response-status response-status-message
	 define-resource resource-uri)
   (only webgate-suspend
	 send/suspend))

(define (webgate-main #!optional (arguments (command-line-arguments)))
  (apply
   (lambda (#!key (listen #f) (backlog 4) (suspension-key #f))
     (cond
      (suspension-key => current-suspension-key))
     (if listen
	 (let ((m (irregex-match
		   '(: (? ($ (+ (~ (":!")))) (":!")) (? ($ (+ (~ (":!")))) (":!")) ($ (+ num)))
		   listen)))
	   (if m
	       (let* ((port
		       (string->number (irregex-match-substring m 3)))
		      (host
		       (cond

			((irregex-match-substring m 2)
			 => (lambda (host) (if (string=? host "*") #f host)))
			(else
			 "localhost")))
		      (protocol
		       (cond


			((irregex-match-substring m 1)
			 => string->symbol)
			(else


			 'scgi))))
		 (case protocol
		   ((scgi)
		    (let ((ear (tcp-listen port backlog host)))
		      (dynamic-wind
			  void
			  (cut scgi-main-loop handle-request ear)
			  (cut tcp-close ear))))
		   ((http)
		    (cond-expand
		     (enable-soup
		      (soup-main-loop handle-request port host))
		     (else
		      (error 'webgate-main "HTTP support not enabled"))))
		   (else
		    (error 'webgate-main "Unknown protocol" protocol))))
	       (error 'webgate-main "Bad listener specification" listen)))
	 (cgi-main-loop handle-request)))
   (map
    (lambda (arg)
      (if (string-prefix? "-" arg)
	  (string->keyword (substring/shared arg 1))
	  arg))
    arguments)))

)