Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
773ab27f
Commit
773ab27f
authored
Jul 23, 1996
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changes by Jim Fulton: pass environ around as arg;
keep_blank_values option to parse().
parent
a48bf799
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
19 deletions
+52
-19
Lib/cgi.py
Lib/cgi.py
+52
-19
No files found.
Lib/cgi.py
View file @
773ab27f
...
@@ -407,16 +407,25 @@ import string
...
@@ -407,16 +407,25 @@ import string
import
sys
import
sys
import
os
import
os
# Parsing functions
# =================
# A shorthand for os.environ
def
parse
(
fp
=
None
,
environ
=
os
.
environ
,
keep_blank_values
=
None
):
environ
=
os
.
environ
"""Parse a query in the environment or from a file (default stdin)
Arguments, all optional:
# Parsing functions
fp : file pointer; default: sys.stdin
# =================
def
parse
(
fp
=
None
):
environ : environment dictionary; default: os.environ
"""Parse a query in the environment or from a file (default stdin)"""
keep_blank_values: flag indicating whether blank values in
URL encoded forms should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
"""
if
not
fp
:
if
not
fp
:
fp
=
sys
.
stdin
fp
=
sys
.
stdin
if
not
environ
.
has_key
(
'REQUEST_METHOD'
):
if
not
environ
.
has_key
(
'REQUEST_METHOD'
):
...
@@ -439,11 +448,23 @@ def parse(fp=None):
...
@@ -439,11 +448,23 @@ def parse(fp=None):
else
:
else
:
qs
=
""
qs
=
""
environ
[
'QUERY_STRING'
]
=
qs
# XXX Shouldn't, really
environ
[
'QUERY_STRING'
]
=
qs
# XXX Shouldn't, really
return
parse_qs
(
qs
)
return
parse_qs
(
qs
,
keep_blank_values
)
def
parse_qs
(
qs
):
def
parse_qs
(
qs
,
keep_blank_values
=
None
):
"""Parse a query given as a string argument"""
"""Parse a query given as a string argumen
Arguments:
qs : URL-encoded query string to be parsed
keep_blank_values: flag indicating whether blank values in
URL encoded queries should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
"""
import
urllib
,
regsub
import
urllib
,
regsub
name_value_pairs
=
string
.
splitfields
(
qs
,
'&'
)
name_value_pairs
=
string
.
splitfields
(
qs
,
'&'
)
dict
=
{}
dict
=
{}
...
@@ -453,7 +474,7 @@ def parse_qs(qs):
...
@@ -453,7 +474,7 @@ def parse_qs(qs):
continue
continue
name
=
nv
[
0
]
name
=
nv
[
0
]
value
=
urllib
.
unquote
(
regsub
.
gsub
(
'+'
,
' '
,
nv
[
1
]))
value
=
urllib
.
unquote
(
regsub
.
gsub
(
'+'
,
' '
,
nv
[
1
]))
if
len
(
value
)
:
if
len
(
value
)
or
keep_blank_values
:
if
dict
.
has_key
(
name
):
if
dict
.
has_key
(
name
):
dict
[
name
].
append
(
value
)
dict
[
name
].
append
(
value
)
else
:
else
:
...
@@ -579,6 +600,7 @@ class MiniFieldStorage:
...
@@ -579,6 +600,7 @@ class MiniFieldStorage:
filename
=
None
filename
=
None
list
=
None
list
=
None
type
=
None
type
=
None
file
=
None
type_options
=
{}
type_options
=
{}
disposition
=
None
disposition
=
None
disposition_options
=
{}
disposition_options
=
{}
...
@@ -589,7 +611,7 @@ class MiniFieldStorage:
...
@@ -589,7 +611,7 @@ class MiniFieldStorage:
from
StringIO
import
StringIO
from
StringIO
import
StringIO
self
.
name
=
name
self
.
name
=
name
self
.
value
=
value
self
.
value
=
value
self
.
file
=
StringIO
(
value
)
#
self.file = StringIO(value)
def
__repr__
(
self
):
def
__repr__
(
self
):
"""Return printable representation."""
"""Return printable representation."""
...
@@ -639,7 +661,8 @@ class FieldStorage:
...
@@ -639,7 +661,8 @@ class FieldStorage:
"""
"""
def
__init__
(
self
,
fp
=
None
,
headers
=
None
,
outerboundary
=
""
):
def
__init__
(
self
,
fp
=
None
,
headers
=
None
,
outerboundary
=
""
,
environ
=
os
.
environ
,
keep_blank_values
=
None
):
"""Constructor. Read multipart/* until last part.
"""Constructor. Read multipart/* until last part.
Arguments, all optional:
Arguments, all optional:
...
@@ -649,11 +672,21 @@ class FieldStorage:
...
@@ -649,11 +672,21 @@ class FieldStorage:
headers : header dictionary-like object; default:
headers : header dictionary-like object; default:
taken from environ as per CGI spec
taken from environ as per CGI spec
outerboundary : optional
terminating multipart boundary
outerboundary :
terminating multipart boundary
(for internal use only)
(for internal use only)
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
URL encoded forms should be treated as blank strings.
A true value inicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
"""
"""
method
=
None
method
=
None
self
.
keep_blank_values
=
keep_blank_values
if
environ
.
has_key
(
'REQUEST_METHOD'
):
if
environ
.
has_key
(
'REQUEST_METHOD'
):
method
=
string
.
upper
(
environ
[
'REQUEST_METHOD'
])
method
=
string
.
upper
(
environ
[
'REQUEST_METHOD'
])
if
not
fp
and
method
==
'GET'
:
if
not
fp
and
method
==
'GET'
:
...
@@ -767,7 +800,7 @@ class FieldStorage:
...
@@ -767,7 +800,7 @@ class FieldStorage:
def
read_urlencoded
(
self
):
def
read_urlencoded
(
self
):
"""Internal: read data in query string format."""
"""Internal: read data in query string format."""
qs
=
self
.
fp
.
read
(
self
.
length
)
qs
=
self
.
fp
.
read
(
self
.
length
)
dict
=
parse_qs
(
q
s
)
dict
=
parse_qs
(
qs
,
self
.
keep_blank_value
s
)
self
.
list
=
[]
self
.
list
=
[]
for
key
,
valuelist
in
dict
.
items
():
for
key
,
valuelist
in
dict
.
items
():
for
value
in
valuelist
:
for
value
in
valuelist
:
...
@@ -922,8 +955,8 @@ class FormContentDict:
...
@@ -922,8 +955,8 @@ class FormContentDict:
form.dict == {key: [val, val, ...], ...}
form.dict == {key: [val, val, ...], ...}
"""
"""
def
__init__
(
self
):
def
__init__
(
self
,
environ
=
os
.
environ
):
self
.
dict
=
parse
(
)
self
.
dict
=
parse
(
environ
)
self
.
query_string
=
environ
[
'QUERY_STRING'
]
self
.
query_string
=
environ
[
'QUERY_STRING'
]
def
__getitem__
(
self
,
key
):
def
__getitem__
(
self
,
key
):
return
self
.
dict
[
key
]
return
self
.
dict
[
key
]
...
@@ -1027,7 +1060,7 @@ class FormContent(FormContentDict):
...
@@ -1027,7 +1060,7 @@ class FormContent(FormContentDict):
# Test/debug code
# Test/debug code
# ===============
# ===============
def
test
():
def
test
(
environ
=
os
.
environ
):
"""Robust test CGI script, usable as main program.
"""Robust test CGI script, usable as main program.
Write minimal HTTP headers and dump all information provided to
Write minimal HTTP headers and dump all information provided to
...
@@ -1041,7 +1074,7 @@ def test():
...
@@ -1041,7 +1074,7 @@ def test():
try
:
try
:
form
=
FieldStorage
()
# Replace with other classes to test those
form
=
FieldStorage
()
# Replace with other classes to test those
print_form
(
form
)
print_form
(
form
)
print_environ
(
)
print_environ
(
environ
)
print_directory
()
print_directory
()
print_arguments
()
print_arguments
()
print_environ_usage
()
print_environ_usage
()
...
@@ -1049,7 +1082,7 @@ def test():
...
@@ -1049,7 +1082,7 @@ def test():
print
"
\
n
\
n
<PRE>"
# Turn off HTML word wrap
print
"
\
n
\
n
<PRE>"
# Turn off HTML word wrap
traceback
.
print_exc
()
traceback
.
print_exc
()
def
print_environ
():
def
print_environ
(
environ
=
os
.
environ
):
"""Dump the shell environment as HTML."""
"""Dump the shell environment as HTML."""
keys
=
environ
.
keys
()
keys
=
environ
.
keys
()
keys
.
sort
()
keys
.
sort
()
...
...
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