1 # Copyright (C) 2008-2009, Eduardo Silva <edsiper@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 def __init__(self
, s
, conf
):
29 # On child end, re-create it
30 signal
.signal(signal
.SIGCHLD
, self
._child
_exit
)
37 opts
= self
.conf
.opts
.split()
41 self
.c
= {'bin': self
.conf
.bin
, 'opts': opts
}
45 [self
.ext_r
, self
.int_w
] = os
.pipe()
46 [self
.int_r
, self
.ext_w
] = os
.pipe()
52 print "My PID " + str(pid
)
53 # Close unused pipe ends
57 # Close unused pipe ends
64 def _child_exit(self
,a
, b
):
69 def write_to_child(self
, message
):
70 os
.write(self
.ext_w
, message
)
80 if buf
[-4:] == '\r\n\r\n':
84 if os
.environ
['PALM_DEBUG'] is not None:
91 def parse_request(self
, data
):
92 arr
= data
.split('\r\n')
96 request
= Request(line
)
106 request
.add_header(key
, val
)
109 if os
.environ
['PALM_DEBUG'] is not None:
110 for h
in request
.headers
:
111 print h
+ ' = \'' + request
.headers
[h
] + '\''
117 def start_child(self
):
118 # Creating epoll for read pipe side
120 remote
, info
= self
._s
.accept()
121 # print "Got connection! I won! ->", os.getpid()
122 buf
= self
.read(remote
)
123 #print "reading, ", buf
125 request
= self
.parse_request(buf
)
127 if self
.c
['bin'] is None:
128 bin
= request
.resource
132 if self
.c
['opts'] is None and bin
!= request
.resource
:
133 opts
= [request
.resource
]
135 opts
= self
.c
['opts']
136 opts
.append(request
.resource
)
138 os
.dup2(remote
.fileno(), sys
.stdout
.fileno())
140 os
.execve(bin
, opts
, request
.headers
)
145 def write_to_parent(self
, message
):
147 n
= os
.write(self
.int_w
, message
)
148 print "Child wrote: ", n
150 def read_data(self
, fd
):
151 buf
= os
.read(fd
, 1024)
152 os
.write(self
.int_w
, buf
)
153 print "Child got: ", buf
159 os
.kill(self
._pid
, signal
.SIGKILL
)
162 def __init__(self
, resource
):
163 self
.resource
= resource
166 def add_header(self
, key
, val
):
167 self
.headers
[key
] = val