Private GIT
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sick-Beard
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vlbox
Sick-Beard
Commits
10c3bc20
Commit
10c3bc20
authored
Jun 11, 2010
by
Nic Wolfe
Browse files
Options
Downloads
Patches
Plain Diff
Committed missing file used to upload NZBs to SAB
parent
447d0e63
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/MultipartPostHandler.py
+89
-0
89 additions, 0 deletions
lib/MultipartPostHandler.py
with
89 additions
and
0 deletions
lib/MultipartPostHandler.py
0 → 100644
+
89
−
0
View file @
10c3bc20
#!/usr/bin/python
####
# 06/2010 Nic Wolfe <nic@wolfeden.ca>
# 02/2006 Will Holcomb <wholcomb@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
import
urllib
import
urllib2
import
mimetools
,
mimetypes
import
os
,
sys
# Controls how sequences are uncoded. If true, elements may be given multiple values by
# assigning a sequence.
doseq
=
1
class
MultipartPostHandler
(
urllib2
.
BaseHandler
):
handler_order
=
urllib2
.
HTTPHandler
.
handler_order
-
10
# needs to run first
def
http_request
(
self
,
request
):
data
=
request
.
get_data
()
if
data
is
not
None
and
type
(
data
)
!=
str
:
v_files
=
[]
v_vars
=
[]
try
:
for
(
key
,
value
)
in
data
.
items
():
if
type
(
value
)
in
(
file
,
list
,
tuple
):
v_files
.
append
((
key
,
value
))
else
:
v_vars
.
append
((
key
,
value
))
except
TypeError
:
systype
,
value
,
traceback
=
sys
.
exc_info
()
raise
TypeError
,
"
not a valid non-string sequence or mapping object
"
,
traceback
if
len
(
v_files
)
==
0
:
data
=
urllib
.
urlencode
(
v_vars
,
doseq
)
else
:
boundary
,
data
=
MultipartPostHandler
.
multipart_encode
(
v_vars
,
v_files
)
contenttype
=
'
multipart/form-data; boundary=%s
'
%
boundary
if
(
request
.
has_header
(
'
Content-Type
'
)
and
request
.
get_header
(
'
Content-Type
'
).
find
(
'
multipart/form-data
'
)
!=
0
):
print
"
Replacing %s with %s
"
%
(
request
.
get_header
(
'
content-type
'
),
'
multipart/form-data
'
)
request
.
add_unredirected_header
(
'
Content-Type
'
,
contenttype
)
request
.
add_data
(
data
)
return
request
@staticmethod
def
multipart_encode
(
vars
,
files
,
boundary
=
None
,
buffer
=
None
):
if
boundary
is
None
:
boundary
=
mimetools
.
choose_boundary
()
if
buffer
is
None
:
buffer
=
''
for
(
key
,
value
)
in
vars
:
buffer
+=
'
--%s
\r\n
'
%
boundary
buffer
+=
'
Content-Disposition: form-data; name=
"
%s
"'
%
key
buffer
+=
'
\r\n\r\n
'
+
value
+
'
\r\n
'
for
(
key
,
fd
)
in
files
:
# allow them to pass in a file or a tuple with name & data
if
type
(
fd
)
==
file
:
name_in
=
fd
.
name
fd
.
seek
(
0
)
data_in
=
fd
.
read
()
elif
type
(
fd
)
in
(
tuple
,
list
):
name_in
,
data_in
=
fd
filename
=
os
.
path
.
basename
(
name_in
)
contenttype
=
mimetypes
.
guess_type
(
filename
)[
0
]
or
'
application/octet-stream
'
buffer
+=
'
--%s
\r\n
'
%
boundary
buffer
+=
'
Content-Disposition: form-data; name=
"
%s
"
; filename=
"
%s
"
\r\n
'
%
(
key
,
filename
)
buffer
+=
'
Content-Type: %s
\r\n
'
%
contenttype
# buffer += 'Content-Length: %s\r\n' % file_size
buffer
+=
'
\r\n
'
+
data_in
+
'
\r\n
'
buffer
+=
'
--%s--
\r\n\r\n
'
%
boundary
return
boundary
,
buffer
https_request
=
http_request
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment