| 
									
										
										
										
											2009-01-07 14:46:52 -05:00
										 |  |  | ! Copyright (C) 2007, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-05-05 05:32:01 -04:00
										 |  |  | USING: namespaces kernel assocs io.files io.streams.duplex | 
					
						
							| 
									
										
										
										
											2009-01-07 14:46:52 -05:00
										 |  |  | combinators arrays io.launcher io.encodings io.encodings.binary io | 
					
						
							| 
									
										
										
										
											2008-09-29 22:18:37 -04:00
										 |  |  | http.server.static http.server http accessors sequences strings | 
					
						
							|  |  |  | math.parser fry urls urls.encoding calendar ;
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | IN: http.server.cgi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cgi-variables ( script-path -- assoc )
 | 
					
						
							|  |  |  |     #! This needs some work. | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         "CGI/1.0" "GATEWAY_INTERFACE" set
 | 
					
						
							|  |  |  |         "HTTP/" request get version>> append "SERVER_PROTOCOL" set
 | 
					
						
							|  |  |  |         "Factor" "SERVER_SOFTWARE" set
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-01 18:22:39 -04:00
										 |  |  |         [ "PATH_TRANSLATED" set ] [ "SCRIPT_FILENAME" set ] bi
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 18:04:20 -04:00
										 |  |  |         url get path>> "SCRIPT_NAME" set
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 18:04:20 -04:00
										 |  |  |         url get host>> "SERVER_NAME" set
 | 
					
						
							|  |  |  |         url get port>> number>string "SERVER_PORT" set
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  |         "" "PATH_INFO" set
 | 
					
						
							|  |  |  |         "" "REMOTE_HOST" set
 | 
					
						
							|  |  |  |         "" "REMOTE_ADDR" set
 | 
					
						
							|  |  |  |         "" "AUTH_TYPE" set
 | 
					
						
							|  |  |  |         "" "REMOTE_USER" set
 | 
					
						
							|  |  |  |         "" "REMOTE_IDENT" set
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request get method>> "REQUEST_METHOD" set
 | 
					
						
							| 
									
										
										
										
											2008-07-09 18:04:20 -04:00
										 |  |  |         url get query>> assoc>query "QUERY_STRING" set
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  |         request get "cookie" header "HTTP_COOKIE" set  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request get "user-agent" header "HTTP_USER_AGENT" set
 | 
					
						
							|  |  |  |         request get "accept" header "HTTP_ACCEPT" set
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-13 23:05:41 -04:00
										 |  |  |         post-request? [ | 
					
						
							| 
									
										
										
										
											2009-01-21 20:55:25 -05:00
										 |  |  |             request get post-data>> data>> | 
					
						
							| 
									
										
										
										
											2008-06-04 20:54:05 -04:00
										 |  |  |             [ "CONTENT_TYPE" set ] | 
					
						
							|  |  |  |             [ length number>string "CONTENT_LENGTH" set ] | 
					
						
							|  |  |  |             bi
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  |         ] when
 | 
					
						
							|  |  |  |     ] H{ } make-assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-07 17:59:44 -05:00
										 |  |  | : <cgi-process> ( name -- desc )
 | 
					
						
							|  |  |  |     <process> | 
					
						
							|  |  |  |         over 1array >>command | 
					
						
							| 
									
										
										
										
											2008-09-29 22:18:37 -04:00
										 |  |  |         swap cgi-variables >>environment | 
					
						
							|  |  |  |         1 minutes >>timeout ;
 | 
					
						
							| 
									
										
										
										
											2008-03-11 04:39:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | : serve-cgi ( name -- response )
 | 
					
						
							|  |  |  |     <raw-response> | 
					
						
							|  |  |  |     200 >>code | 
					
						
							|  |  |  |     "CGI output follows" >>message | 
					
						
							| 
									
										
										
										
											2008-03-11 04:39:09 -04:00
										 |  |  |     swap '[ | 
					
						
							| 
									
										
										
										
											2009-01-07 14:46:52 -05:00
										 |  |  |         binary encode-output | 
					
						
							| 
									
										
										
										
											2009-03-07 23:09:57 -05:00
										 |  |  |         output-stream get _ <cgi-process> binary <process-stream> [ | 
					
						
							| 
									
										
										
										
											2009-01-21 20:55:25 -05:00
										 |  |  |             post-request? [ request get post-data>> data>> write flush ] when
 | 
					
						
							| 
									
										
										
										
											2009-03-07 23:09:57 -05:00
										 |  |  |             '[ _ stream-write ] each-block
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  |         ] with-stream | 
					
						
							| 
									
										
										
										
											2008-03-11 04:39:09 -04:00
										 |  |  |     ] >>body ;
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-19 01:52:41 -05:00
										 |  |  | SLOT: special | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-29 01:57:38 -05:00
										 |  |  | : enable-cgi ( responder -- responder )
 | 
					
						
							|  |  |  |     [ serve-cgi ] "application/x-cgi-script" | 
					
						
							|  |  |  |     pick special>> set-at ;
 |