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 # Close unused pipe ends
56 # Close unused pipe ends
63 def _child_exit(self
,a
, b
):
68 def write_to_child(self
, message
):
69 os
.write(self
.ext_w
, message
)
79 if buf
[-4:] == '\r\n\r\n':
83 if os
.environ
['PALM_DEBUG'] is not None:
90 def parse_request(self
, data
):
91 arr
= data
.split('\r\n')
95 request
= Request(line
)
105 request
.add_header(key
, val
)
108 if os
.environ
['PALM_DEBUG'] is not None:
109 for h
in request
.headers
:
110 print h
+ ' = \'' + request
.headers
[h
] + '\''
116 def start_child(self
):
117 # Creating epoll for read pipe side
119 remote
, info
= self
._s
.accept()
120 # print "Got connection! I won! ->", os.getpid()
121 buf
= self
.read(remote
)
122 #print "reading, ", buf
124 request
= self
.parse_request(buf
)
126 if self
.c
['bin'] is None:
127 bin
= request
.resource
131 if self
.c
['opts'] is None and bin
!= request
.resource
:
132 opts
= [request
.resource
]
134 opts
= self
.c
['opts']
135 opts
.append(request
.resource
)
137 os
.dup2(remote
.fileno(), sys
.stdout
.fileno())
139 os
.execve(bin
, opts
, request
.headers
)
144 def write_to_parent(self
, message
):
146 n
= os
.write(self
.int_w
, message
)
147 print "Child wrote: ", n
149 def read_data(self
, fd
):
150 buf
= os
.read(fd
, 1024)
151 os
.write(self
.int_w
, buf
)
152 print "Child got: ", buf
158 os
.kill(self
._pid
, signal
.SIGKILL
)
161 def __init__(self
, resource
):
162 self
.resource
= resource
165 def add_header(self
, key
, val
):
166 self
.headers
[key
] = val