Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
cd853447
Commit
cd853447
authored
Jan 13, 1999
by
Amos Latteier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Major work on zope handler. Now uses new future producer facility and
sized input for STDIN collection. It's not perfect yet.
parent
edd63d35
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
252 additions
and
134 deletions
+252
-134
ZServer/zope_handler.py
ZServer/zope_handler.py
+126
-67
lib/python/ZServer/zope_handler.py
lib/python/ZServer/zope_handler.py
+126
-67
No files found.
ZServer/zope_handler.py
View file @
cd853447
...
@@ -31,7 +31,7 @@ unquote = default_handler.unquote
...
@@ -31,7 +31,7 @@ unquote = default_handler.unquote
get_header
=
default_handler
.
get_header
get_header
=
default_handler
.
get_header
CONTENT_LENGTH
=
regex
.
compile
(
'Content-Length:
\
([
0
-9]+
\
)
'
,regex.casefold)
CONTENT_LENGTH
=
regex
.
compile
(
'Content-Length:
\
([
0
-9]+
\
)
'
,regex.casefold)
CONNECTION = regex.compile ('
Connection
:
\
(.
*
\
)
', regex.casefold)
# maps request headers to environment variables
# maps request headers to environment variables
#
#
...
@@ -76,8 +76,11 @@ class zope_handler:
...
@@ -76,8 +76,11 @@ class zope_handler:
def handle_request(self,request):
def handle_request(self,request):
self.hits.increment()
self.hits.increment()
if request.command in ["post","put"]:
if request.command in ["post","put"]:
request.collector=input_collector(self,request)
cl=get_header(CONTENT_LENGTH, request.header)
request.channel.set_terminator(None)
self.data = StringIO() # XXX maybe use tempfile, if cl is large
self.request = request
request.channel.set_terminator(string.atoi(cl))
request.collector=self
else:
else:
sin=StringIO()
sin=StringIO()
self.continue_request(sin,request)
self.continue_request(sin,request)
...
@@ -124,45 +127,58 @@ class zope_handler:
...
@@ -124,45 +127,58 @@ class zope_handler:
def
continue_request
(
self
,
sin
,
request
):
def
continue_request
(
self
,
sin
,
request
):
"continue handling request now that we have the stdin"
"continue handling request now that we have the stdin"
request
.
data
=
[]
outpipe
=
handle
(
self
.
module_name
,
self
.
get_environment
(
request
),
sin
)
outpipe
=
handle
(
self
.
module_name
,
self
.
get_environment
(
request
),
sin
,(
self
.
more
,(
request
,)))
def
more
(
self
,
request
,
pipe
):
"""packages up the current output from the output pipe.
called repeatedly when there is something in the output pipe"""
# XXX does not stream, probably inefficient.
done
=
None
while
not
done
:
data
=
pipe
.
read
()
if
data
is
None
:
done
=
1
elif
data
==
""
:
done
=
1
self
.
finish
(
request
)
else
:
request
.
data
.
append
(
data
)
def
finish
(
self
,
request
):
# now comes the hairy stuff. adapted from http_server
"finishes up the response"
#
connection
=
string
.
lower
(
get_header
(
CONNECTION
,
request
.
header
))
# XXX does not stream, probably inefficient to boot
close_it
=
0
wrap_in_chunking
=
0
data
=
string
.
join
(
request
.
data
,
''
)
if
request
.
version
==
'1.0'
:
if
string
.
find
(
data
,
"
\
n
\
n
"
):
if
connection
==
'keep-alive'
:
[
headers
,
html
]
=
string
.
split
(
data
,
"
\
n
\
n
"
,
1
)
if
not
request
.
has_key
(
'Content-Length'
):
headers
=
string
.
split
(
headers
,
"
\
n
"
)
close_it
=
1
for
line
in
headers
:
else
:
[
header
,
header_value
]
=
string
.
split
(
line
,
": "
,
1
)
request
[
'Connection'
]
=
'Keep-Alive'
if
header
==
"Status"
:
else
:
[
code
,
message
]
=
string
.
split
(
header_value
,
" "
,
1
)
close_it
=
1
request
.
reply_code
=
string
.
atoi
(
code
)
elif
request
.
version
==
'1.1'
:
if
connection
==
'close'
:
close_it
=
1
elif
not
request
.
has_key
(
'Content-Length'
):
if
request
.
has_key
(
'Transfer-Encoding'
):
if
not
request
[
'Transfer-Encoding'
]
==
'chunked'
:
close_it
=
1
elif
request
.
use_chunked
:
request
[
'Transfer-Encoding'
]
=
'chunked'
wrap_in_chunking
=
1
else
:
else
:
request
[
header
]
=
header_value
close_it
=
1
request
.
push
(
html
)
request
.
done
()
if
close_it
:
request
[
'Connection'
]
=
'close'
outgoing_producer
=
crazy_producer
(
request
,
outpipe
)
# apply a few final transformations to the output
request
.
channel
.
push_with_producer
(
# globbing gives us large packets
producers
.
globbing_producer
(
# hooking lets us log the number of bytes sent
producers
.
hooked_producer
(
outgoing_producer
,
request
.
log
)
)
)
request
.
channel
.
current_request
=
None
if
close_it
:
request
.
channel
.
close_when_done
()
def
status
(
self
):
def
status
(
self
):
return
producers
.
simple_producer
(
"""
return
producers
.
simple_producer
(
"""
...
@@ -173,32 +189,75 @@ class zope_handler:
...
@@ -173,32 +189,75 @@ class zope_handler:
</ul>"""
%
(
self
.
module_name
,
int
(
self
.
hits
))
</ul>"""
%
(
self
.
module_name
,
int
(
self
.
hits
))
)
)
# put and post collection methods
class
input_collector
:
#
"gathers input for put and post requests"
# XXX update to use tempfiles for large content
def
__init__
(
self
,
handler
,
request
):
self
.
handler
=
handler
self
.
request
=
request
self
.
data
=
StringIO
()
# make sure there's a content-length header
self
.
cl
=
get_header
(
CONTENT_LENGTH
,
request
.
header
)
if
not
self
.
cl
:
request
.
error
(
411
)
return
else
:
self
.
cl
=
string
.
atoi
(
self
.
cl
)
def
collect_incoming_data
(
self
,
data
):
def
collect_incoming_data
(
self
,
data
):
self
.
data
.
write
(
data
)
self
.
data
.
write
(
data
)
if
self
.
data
.
tell
()
>=
self
.
cl
:
def
found_terminator
(
self
):
# reset collector
self
.
request
.
channel
.
set_terminator
(
'
\
r
\
n
\
r
\
n
'
)
self
.
request
.
collector
=
None
# finish request
self
.
data
.
seek
(
0
)
self
.
data
.
seek
(
0
)
h
=
self
.
handler
r
=
self
.
request
r
=
self
.
request
# set the terminator back to the default
d
=
self
.
data
self
.
request
.
channel
.
set_terminator
(
'
\
r
\
n
\
r
\
n
'
)
del
self
.
handler
del
self
.
request
del
self
.
request
h
.
continue_request
(
self
.
data
,
r
)
del
self
.
data
self
.
continue_request
(
d
,
r
)
class
pipe_producer
:
def
__init__
(
self
,
pipe
):
self
.
pipe
=
pipe
def
more
(
self
):
#if not self.ready():
# import traceback
# traceback.print_stack()
data
=
self
.
pipe
.
read
()
if
data
is
None
:
return
''
return
data
def
ready
(
self
):
return
self
.
pipe
.
ready
()
class
crazy_producer
:
"""This weird producer patches together
medusa's idea of http headers with ZPublisher's
"""
def
__init__
(
self
,
request
,
pipe
):
self
.
request
=
request
self
.
pipe
=
pipe
self
.
done
=
None
self
.
buffer
=
''
def
more
(
self
):
if
self
.
buffer
:
b
=
self
.
buffer
self
.
buffer
=
''
return
b
data
=
self
.
pipe
.
read
()
if
data
is
None
:
return
''
return
data
def
ready
(
self
):
if
self
.
done
:
return
self
.
pipe
.
ready
()
elif
self
.
pipe
.
ready
():
self
.
buffer
=
self
.
buffer
+
self
.
pipe
.
read
()
if
string
.
find
(
self
.
buffer
,
"
\
n
\
n
"
):
[
headers
,
html
]
=
string
.
split
(
self
.
buffer
,
"
\
n
\
n
"
,
1
)
headers
=
string
.
split
(
headers
,
"
\
n
"
)
for
line
in
headers
:
[
header
,
header_value
]
=
string
.
split
(
line
,
": "
,
1
)
if
header
==
"Status"
:
[
code
,
message
]
=
string
.
split
(
header_value
,
" "
,
1
)
self
.
request
.
reply_code
=
string
.
atoi
(
code
)
else
:
self
.
request
[
header
]
=
header_value
self
.
buffer
=
self
.
request
.
build_reply_header
()
+
html
del
self
.
request
self
.
done
=
1
lib/python/ZServer/zope_handler.py
View file @
cd853447
...
@@ -31,7 +31,7 @@ unquote = default_handler.unquote
...
@@ -31,7 +31,7 @@ unquote = default_handler.unquote
get_header
=
default_handler
.
get_header
get_header
=
default_handler
.
get_header
CONTENT_LENGTH
=
regex
.
compile
(
'Content-Length:
\
([
0
-9]+
\
)
'
,regex.casefold)
CONTENT_LENGTH
=
regex
.
compile
(
'Content-Length:
\
([
0
-9]+
\
)
'
,regex.casefold)
CONNECTION = regex.compile ('
Connection
:
\
(.
*
\
)
', regex.casefold)
# maps request headers to environment variables
# maps request headers to environment variables
#
#
...
@@ -76,8 +76,11 @@ class zope_handler:
...
@@ -76,8 +76,11 @@ class zope_handler:
def handle_request(self,request):
def handle_request(self,request):
self.hits.increment()
self.hits.increment()
if request.command in ["post","put"]:
if request.command in ["post","put"]:
request.collector=input_collector(self,request)
cl=get_header(CONTENT_LENGTH, request.header)
request.channel.set_terminator(None)
self.data = StringIO() # XXX maybe use tempfile, if cl is large
self.request = request
request.channel.set_terminator(string.atoi(cl))
request.collector=self
else:
else:
sin=StringIO()
sin=StringIO()
self.continue_request(sin,request)
self.continue_request(sin,request)
...
@@ -124,45 +127,58 @@ class zope_handler:
...
@@ -124,45 +127,58 @@ class zope_handler:
def
continue_request
(
self
,
sin
,
request
):
def
continue_request
(
self
,
sin
,
request
):
"continue handling request now that we have the stdin"
"continue handling request now that we have the stdin"
request
.
data
=
[]
outpipe
=
handle
(
self
.
module_name
,
self
.
get_environment
(
request
),
sin
)
outpipe
=
handle
(
self
.
module_name
,
self
.
get_environment
(
request
),
sin
,(
self
.
more
,(
request
,)))
def
more
(
self
,
request
,
pipe
):
"""packages up the current output from the output pipe.
called repeatedly when there is something in the output pipe"""
# XXX does not stream, probably inefficient.
done
=
None
while
not
done
:
data
=
pipe
.
read
()
if
data
is
None
:
done
=
1
elif
data
==
""
:
done
=
1
self
.
finish
(
request
)
else
:
request
.
data
.
append
(
data
)
def
finish
(
self
,
request
):
# now comes the hairy stuff. adapted from http_server
"finishes up the response"
#
connection
=
string
.
lower
(
get_header
(
CONNECTION
,
request
.
header
))
# XXX does not stream, probably inefficient to boot
close_it
=
0
wrap_in_chunking
=
0
data
=
string
.
join
(
request
.
data
,
''
)
if
request
.
version
==
'1.0'
:
if
string
.
find
(
data
,
"
\
n
\
n
"
):
if
connection
==
'keep-alive'
:
[
headers
,
html
]
=
string
.
split
(
data
,
"
\
n
\
n
"
,
1
)
if
not
request
.
has_key
(
'Content-Length'
):
headers
=
string
.
split
(
headers
,
"
\
n
"
)
close_it
=
1
for
line
in
headers
:
else
:
[
header
,
header_value
]
=
string
.
split
(
line
,
": "
,
1
)
request
[
'Connection'
]
=
'Keep-Alive'
if
header
==
"Status"
:
else
:
[
code
,
message
]
=
string
.
split
(
header_value
,
" "
,
1
)
close_it
=
1
request
.
reply_code
=
string
.
atoi
(
code
)
elif
request
.
version
==
'1.1'
:
if
connection
==
'close'
:
close_it
=
1
elif
not
request
.
has_key
(
'Content-Length'
):
if
request
.
has_key
(
'Transfer-Encoding'
):
if
not
request
[
'Transfer-Encoding'
]
==
'chunked'
:
close_it
=
1
elif
request
.
use_chunked
:
request
[
'Transfer-Encoding'
]
=
'chunked'
wrap_in_chunking
=
1
else
:
else
:
request
[
header
]
=
header_value
close_it
=
1
request
.
push
(
html
)
request
.
done
()
if
close_it
:
request
[
'Connection'
]
=
'close'
outgoing_producer
=
crazy_producer
(
request
,
outpipe
)
# apply a few final transformations to the output
request
.
channel
.
push_with_producer
(
# globbing gives us large packets
producers
.
globbing_producer
(
# hooking lets us log the number of bytes sent
producers
.
hooked_producer
(
outgoing_producer
,
request
.
log
)
)
)
request
.
channel
.
current_request
=
None
if
close_it
:
request
.
channel
.
close_when_done
()
def
status
(
self
):
def
status
(
self
):
return
producers
.
simple_producer
(
"""
return
producers
.
simple_producer
(
"""
...
@@ -173,32 +189,75 @@ class zope_handler:
...
@@ -173,32 +189,75 @@ class zope_handler:
</ul>"""
%
(
self
.
module_name
,
int
(
self
.
hits
))
</ul>"""
%
(
self
.
module_name
,
int
(
self
.
hits
))
)
)
# put and post collection methods
class
input_collector
:
#
"gathers input for put and post requests"
# XXX update to use tempfiles for large content
def
__init__
(
self
,
handler
,
request
):
self
.
handler
=
handler
self
.
request
=
request
self
.
data
=
StringIO
()
# make sure there's a content-length header
self
.
cl
=
get_header
(
CONTENT_LENGTH
,
request
.
header
)
if
not
self
.
cl
:
request
.
error
(
411
)
return
else
:
self
.
cl
=
string
.
atoi
(
self
.
cl
)
def
collect_incoming_data
(
self
,
data
):
def
collect_incoming_data
(
self
,
data
):
self
.
data
.
write
(
data
)
self
.
data
.
write
(
data
)
if
self
.
data
.
tell
()
>=
self
.
cl
:
def
found_terminator
(
self
):
# reset collector
self
.
request
.
channel
.
set_terminator
(
'
\
r
\
n
\
r
\
n
'
)
self
.
request
.
collector
=
None
# finish request
self
.
data
.
seek
(
0
)
self
.
data
.
seek
(
0
)
h
=
self
.
handler
r
=
self
.
request
r
=
self
.
request
# set the terminator back to the default
d
=
self
.
data
self
.
request
.
channel
.
set_terminator
(
'
\
r
\
n
\
r
\
n
'
)
del
self
.
handler
del
self
.
request
del
self
.
request
h
.
continue_request
(
self
.
data
,
r
)
del
self
.
data
self
.
continue_request
(
d
,
r
)
class
pipe_producer
:
def
__init__
(
self
,
pipe
):
self
.
pipe
=
pipe
def
more
(
self
):
#if not self.ready():
# import traceback
# traceback.print_stack()
data
=
self
.
pipe
.
read
()
if
data
is
None
:
return
''
return
data
def
ready
(
self
):
return
self
.
pipe
.
ready
()
class
crazy_producer
:
"""This weird producer patches together
medusa's idea of http headers with ZPublisher's
"""
def
__init__
(
self
,
request
,
pipe
):
self
.
request
=
request
self
.
pipe
=
pipe
self
.
done
=
None
self
.
buffer
=
''
def
more
(
self
):
if
self
.
buffer
:
b
=
self
.
buffer
self
.
buffer
=
''
return
b
data
=
self
.
pipe
.
read
()
if
data
is
None
:
return
''
return
data
def
ready
(
self
):
if
self
.
done
:
return
self
.
pipe
.
ready
()
elif
self
.
pipe
.
ready
():
self
.
buffer
=
self
.
buffer
+
self
.
pipe
.
read
()
if
string
.
find
(
self
.
buffer
,
"
\
n
\
n
"
):
[
headers
,
html
]
=
string
.
split
(
self
.
buffer
,
"
\
n
\
n
"
,
1
)
headers
=
string
.
split
(
headers
,
"
\
n
"
)
for
line
in
headers
:
[
header
,
header_value
]
=
string
.
split
(
line
,
": "
,
1
)
if
header
==
"Status"
:
[
code
,
message
]
=
string
.
split
(
header_value
,
" "
,
1
)
self
.
request
.
reply_code
=
string
.
atoi
(
code
)
else
:
self
.
request
[
header
]
=
header_value
self
.
buffer
=
self
.
request
.
build_reply_header
()
+
html
del
self
.
request
self
.
done
=
1
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment