multipart-decoder 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- multipart_decoder-0.0.1/LICENSE +9 -0
- multipart_decoder-0.0.1/PKG-INFO +119 -0
- multipart_decoder-0.0.1/README.md +104 -0
- multipart_decoder-0.0.1/pyproject.toml +23 -0
- multipart_decoder-0.0.1/setup.cfg +4 -0
- multipart_decoder-0.0.1/src/multipart_decoder/__init__.py +8 -0
- multipart_decoder-0.0.1/src/multipart_decoder/decoder.py +71 -0
- multipart_decoder-0.0.1/src/multipart_decoder.egg-info/PKG-INFO +119 -0
- multipart_decoder-0.0.1/src/multipart_decoder.egg-info/SOURCES.txt +10 -0
- multipart_decoder-0.0.1/src/multipart_decoder.egg-info/dependency_links.txt +1 -0
- multipart_decoder-0.0.1/src/multipart_decoder.egg-info/top_level.txt +1 -0
- multipart_decoder-0.0.1/tests/test_decoder.py +72 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Satya Tulasi Jalandhar C H
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: multipart-decoder
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A lightweight Python package for decoding multipart/form-data content
|
|
5
|
+
Author-email: Satya Tulasi Jalandhar C H <satyatulasijalandharch2@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/satyatulasijalandharch/multipart-decoder
|
|
8
|
+
Project-URL: Issues, https://github.com/satyatulasijalandharch/multipart-decoder/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# multipart_decoder
|
|
17
|
+
|
|
18
|
+
A lightweight Python package for decoding multipart/form-data content.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install multipart-decoder
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
**Basic Usage Example for multipart_decoder**
|
|
28
|
+
|
|
29
|
+
This example demonstrates how to parse simple form data with text fields.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
|
|
33
|
+
from multipart_decoder import MultipartDecoder
|
|
34
|
+
|
|
35
|
+
# Example with raw form data
|
|
36
|
+
def basic_form_parsing():
|
|
37
|
+
# In a real application, these would come from an HTTP request
|
|
38
|
+
content_type = "multipart/form-data; boundary=----WebKitFormBoundarydVTl2EAp1scXYsWZ"
|
|
39
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5ZFZUbDJFQXAxc2NYWXNXWg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ1c2VybmFtZSINCg0KZXhhbXBsZV91c2VyDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImVtYWlsIg0KDQp1c2VyQGV4YW1wbGUuY29tDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daLS0NCg=="
|
|
40
|
+
|
|
41
|
+
# Parse the multipart form data
|
|
42
|
+
form_data = MultipartDecoder(body, content_type)
|
|
43
|
+
|
|
44
|
+
# Access the form fields
|
|
45
|
+
print(f"Username: {form_data['username']}")
|
|
46
|
+
print(f"Email: {form_data['email']}")
|
|
47
|
+
|
|
48
|
+
# Show the entire parsed data structure
|
|
49
|
+
print("\nComplete form data dictionary:")
|
|
50
|
+
print(form_data)
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
basic_form_parsing()
|
|
54
|
+
```
|
|
55
|
+
## Expected Output
|
|
56
|
+
|
|
57
|
+
### Basic Form Parsing:
|
|
58
|
+
- **Username**: `example_user`
|
|
59
|
+
- **Email**: `user@example.com`
|
|
60
|
+
|
|
61
|
+
#### Complete Form Data Dictionary:
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"username": "example_user",
|
|
65
|
+
"email": "user@example.com"
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
**File Upload Example for multipart_decoder**
|
|
71
|
+
|
|
72
|
+
This example demonstrates how to handle file uploads in multipart form data.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from multipart_decoder import MultipartDecoder
|
|
76
|
+
import os
|
|
77
|
+
|
|
78
|
+
def process_file_upload():
|
|
79
|
+
"""Process an example multipart form with file uploads."""
|
|
80
|
+
content_type = (
|
|
81
|
+
"multipart/form-data; boundary=----WebKitFormBoundary4AQwYOUUCEUw9hsX"
|
|
82
|
+
)
|
|
83
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnk0QVF3WU9VVUNFVXc5aHNYLS0NCg=="
|
|
84
|
+
|
|
85
|
+
form_data = MultipartDecoder(body, content_type)
|
|
86
|
+
# Process regular form field
|
|
87
|
+
print(f"Field 1: {form_data['field1']}")
|
|
88
|
+
print(f"Field 2: {form_data['field2']}")
|
|
89
|
+
|
|
90
|
+
# Process file uploads
|
|
91
|
+
if 'file1' in form_data:
|
|
92
|
+
filename, content_type, file_data = form_data['file1']
|
|
93
|
+
print(f" - Filename: {filename}")
|
|
94
|
+
print(f" - Content type: {content_type}")
|
|
95
|
+
print(f" - File size: {len(file_data)} bytes")
|
|
96
|
+
|
|
97
|
+
# Example: save the file
|
|
98
|
+
save_path = f"saved_{filename}"
|
|
99
|
+
with open(save_path, "wb") as f:
|
|
100
|
+
f.write(file_data)
|
|
101
|
+
print(f" - File saved to: {save_path}")
|
|
102
|
+
|
|
103
|
+
# # After using the file, delete it for this example
|
|
104
|
+
os.remove(save_path)
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
process_file_upload()
|
|
108
|
+
```
|
|
109
|
+
# Expected Output:
|
|
110
|
+
|
|
111
|
+
### Test Fields:
|
|
112
|
+
- **Field 1**: `value1`
|
|
113
|
+
- **Field 2**: `value2`
|
|
114
|
+
|
|
115
|
+
### File Field:
|
|
116
|
+
- **Filename**: `example.txt`
|
|
117
|
+
- **Content Type**: `text/plain`
|
|
118
|
+
- **File Size**: `13 bytes`
|
|
119
|
+
- **File Saved To**: `saved_example.txt`
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# multipart_decoder
|
|
2
|
+
|
|
3
|
+
A lightweight Python package for decoding multipart/form-data content.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install multipart-decoder
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
**Basic Usage Example for multipart_decoder**
|
|
13
|
+
|
|
14
|
+
This example demonstrates how to parse simple form data with text fields.
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
|
|
18
|
+
from multipart_decoder import MultipartDecoder
|
|
19
|
+
|
|
20
|
+
# Example with raw form data
|
|
21
|
+
def basic_form_parsing():
|
|
22
|
+
# In a real application, these would come from an HTTP request
|
|
23
|
+
content_type = "multipart/form-data; boundary=----WebKitFormBoundarydVTl2EAp1scXYsWZ"
|
|
24
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5ZFZUbDJFQXAxc2NYWXNXWg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ1c2VybmFtZSINCg0KZXhhbXBsZV91c2VyDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImVtYWlsIg0KDQp1c2VyQGV4YW1wbGUuY29tDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daLS0NCg=="
|
|
25
|
+
|
|
26
|
+
# Parse the multipart form data
|
|
27
|
+
form_data = MultipartDecoder(body, content_type)
|
|
28
|
+
|
|
29
|
+
# Access the form fields
|
|
30
|
+
print(f"Username: {form_data['username']}")
|
|
31
|
+
print(f"Email: {form_data['email']}")
|
|
32
|
+
|
|
33
|
+
# Show the entire parsed data structure
|
|
34
|
+
print("\nComplete form data dictionary:")
|
|
35
|
+
print(form_data)
|
|
36
|
+
|
|
37
|
+
if __name__ == "__main__":
|
|
38
|
+
basic_form_parsing()
|
|
39
|
+
```
|
|
40
|
+
## Expected Output
|
|
41
|
+
|
|
42
|
+
### Basic Form Parsing:
|
|
43
|
+
- **Username**: `example_user`
|
|
44
|
+
- **Email**: `user@example.com`
|
|
45
|
+
|
|
46
|
+
#### Complete Form Data Dictionary:
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"username": "example_user",
|
|
50
|
+
"email": "user@example.com"
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
**File Upload Example for multipart_decoder**
|
|
56
|
+
|
|
57
|
+
This example demonstrates how to handle file uploads in multipart form data.
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from multipart_decoder import MultipartDecoder
|
|
61
|
+
import os
|
|
62
|
+
|
|
63
|
+
def process_file_upload():
|
|
64
|
+
"""Process an example multipart form with file uploads."""
|
|
65
|
+
content_type = (
|
|
66
|
+
"multipart/form-data; boundary=----WebKitFormBoundary4AQwYOUUCEUw9hsX"
|
|
67
|
+
)
|
|
68
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnk0QVF3WU9VVUNFVXc5aHNYLS0NCg=="
|
|
69
|
+
|
|
70
|
+
form_data = MultipartDecoder(body, content_type)
|
|
71
|
+
# Process regular form field
|
|
72
|
+
print(f"Field 1: {form_data['field1']}")
|
|
73
|
+
print(f"Field 2: {form_data['field2']}")
|
|
74
|
+
|
|
75
|
+
# Process file uploads
|
|
76
|
+
if 'file1' in form_data:
|
|
77
|
+
filename, content_type, file_data = form_data['file1']
|
|
78
|
+
print(f" - Filename: {filename}")
|
|
79
|
+
print(f" - Content type: {content_type}")
|
|
80
|
+
print(f" - File size: {len(file_data)} bytes")
|
|
81
|
+
|
|
82
|
+
# Example: save the file
|
|
83
|
+
save_path = f"saved_{filename}"
|
|
84
|
+
with open(save_path, "wb") as f:
|
|
85
|
+
f.write(file_data)
|
|
86
|
+
print(f" - File saved to: {save_path}")
|
|
87
|
+
|
|
88
|
+
# # After using the file, delete it for this example
|
|
89
|
+
os.remove(save_path)
|
|
90
|
+
|
|
91
|
+
if __name__ == "__main__":
|
|
92
|
+
process_file_upload()
|
|
93
|
+
```
|
|
94
|
+
# Expected Output:
|
|
95
|
+
|
|
96
|
+
### Test Fields:
|
|
97
|
+
- **Field 1**: `value1`
|
|
98
|
+
- **Field 2**: `value2`
|
|
99
|
+
|
|
100
|
+
### File Field:
|
|
101
|
+
- **Filename**: `example.txt`
|
|
102
|
+
- **Content Type**: `text/plain`
|
|
103
|
+
- **File Size**: `13 bytes`
|
|
104
|
+
- **File Saved To**: `saved_example.txt`
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "multipart-decoder"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Satya Tulasi Jalandhar C H", email="satyatulasijalandharch2@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "A lightweight Python package for decoding multipart/form-data content"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
license = "MIT"
|
|
19
|
+
license-files = ["LICENSE*"]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/satyatulasijalandharch/multipart-decoder"
|
|
23
|
+
Issues = "https://github.com/satyatulasijalandharch/multipart-decoder/issues"
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Decoder module for parsing multipart/form-data content.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
import base64
|
|
7
|
+
import email.parser
|
|
8
|
+
from io import BytesIO
|
|
9
|
+
from email.policy import default
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def MultipartDecoder(content_bytes, content_type):
|
|
13
|
+
"""
|
|
14
|
+
Decode multipart form-data from bytes content.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
content_bytes (bytes): The raw multipart form-data content
|
|
18
|
+
content_type (str): The Content-Type header value (must include boundary)
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
dict: A dictionary where keys are field names and values are either:
|
|
22
|
+
- strings for regular form fields
|
|
23
|
+
- tuples of (filename, content_type, data) for file uploads
|
|
24
|
+
|
|
25
|
+
Raises:
|
|
26
|
+
ValueError: If the content is not multipart or lacks required headers
|
|
27
|
+
"""
|
|
28
|
+
if not content_type or 'boundary=' not in content_type:
|
|
29
|
+
raise ValueError("Content-Type header must include a boundary parameter")
|
|
30
|
+
|
|
31
|
+
try:
|
|
32
|
+
content_bytes = base64.b64decode(content_bytes)
|
|
33
|
+
except (base64.binascii.Error, TypeError) as e:
|
|
34
|
+
raise ValueError("Failed to decode base64 content") from e
|
|
35
|
+
# Prepare the content with proper headers for the email parser
|
|
36
|
+
content = BytesIO()
|
|
37
|
+
content.write(b'Content-Type: ' + content_type.encode('utf-8') + b'\r\n\r\n')
|
|
38
|
+
content.write(content_bytes)
|
|
39
|
+
content.seek(0)
|
|
40
|
+
|
|
41
|
+
# Parse the multipart content
|
|
42
|
+
parser = email.parser.BytesParser(policy=default)
|
|
43
|
+
message = parser.parse(content)
|
|
44
|
+
|
|
45
|
+
if not message.is_multipart():
|
|
46
|
+
raise ValueError("Content is not multipart")
|
|
47
|
+
|
|
48
|
+
form_data = {}
|
|
49
|
+
|
|
50
|
+
# Process each part of the multipart message
|
|
51
|
+
for part in message.get_payload():
|
|
52
|
+
# Get the Content-Disposition header to extract field name and filename
|
|
53
|
+
content_disposition = part.get("Content-Disposition", "")
|
|
54
|
+
# Use regex to extract field name from content disposition
|
|
55
|
+
name_match = re.search(r'name="([^"]*)"', content_disposition)
|
|
56
|
+
|
|
57
|
+
if name_match:
|
|
58
|
+
field_name = name_match.group(1)
|
|
59
|
+
filename_match = re.search(r'filename="([^"]*)"', content_disposition)
|
|
60
|
+
|
|
61
|
+
if filename_match:
|
|
62
|
+
# This is a file upload
|
|
63
|
+
filename = filename_match.group(1)
|
|
64
|
+
part_content_type = part.get_content_type()
|
|
65
|
+
data = part.get_payload(decode=True) # Binary file content
|
|
66
|
+
form_data[field_name] = (filename, part_content_type, data)
|
|
67
|
+
else:
|
|
68
|
+
# This is a regular form field
|
|
69
|
+
form_data[field_name] = part.get_payload(decode=True).decode(part.get_content_charset('utf-8'))
|
|
70
|
+
|
|
71
|
+
return form_data
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: multipart-decoder
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A lightweight Python package for decoding multipart/form-data content
|
|
5
|
+
Author-email: Satya Tulasi Jalandhar C H <satyatulasijalandharch2@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/satyatulasijalandharch/multipart-decoder
|
|
8
|
+
Project-URL: Issues, https://github.com/satyatulasijalandharch/multipart-decoder/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# multipart_decoder
|
|
17
|
+
|
|
18
|
+
A lightweight Python package for decoding multipart/form-data content.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install multipart-decoder
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
**Basic Usage Example for multipart_decoder**
|
|
28
|
+
|
|
29
|
+
This example demonstrates how to parse simple form data with text fields.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
|
|
33
|
+
from multipart_decoder import MultipartDecoder
|
|
34
|
+
|
|
35
|
+
# Example with raw form data
|
|
36
|
+
def basic_form_parsing():
|
|
37
|
+
# In a real application, these would come from an HTTP request
|
|
38
|
+
content_type = "multipart/form-data; boundary=----WebKitFormBoundarydVTl2EAp1scXYsWZ"
|
|
39
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5ZFZUbDJFQXAxc2NYWXNXWg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ1c2VybmFtZSINCg0KZXhhbXBsZV91c2VyDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImVtYWlsIg0KDQp1c2VyQGV4YW1wbGUuY29tDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daLS0NCg=="
|
|
40
|
+
|
|
41
|
+
# Parse the multipart form data
|
|
42
|
+
form_data = MultipartDecoder(body, content_type)
|
|
43
|
+
|
|
44
|
+
# Access the form fields
|
|
45
|
+
print(f"Username: {form_data['username']}")
|
|
46
|
+
print(f"Email: {form_data['email']}")
|
|
47
|
+
|
|
48
|
+
# Show the entire parsed data structure
|
|
49
|
+
print("\nComplete form data dictionary:")
|
|
50
|
+
print(form_data)
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
basic_form_parsing()
|
|
54
|
+
```
|
|
55
|
+
## Expected Output
|
|
56
|
+
|
|
57
|
+
### Basic Form Parsing:
|
|
58
|
+
- **Username**: `example_user`
|
|
59
|
+
- **Email**: `user@example.com`
|
|
60
|
+
|
|
61
|
+
#### Complete Form Data Dictionary:
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"username": "example_user",
|
|
65
|
+
"email": "user@example.com"
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
**File Upload Example for multipart_decoder**
|
|
71
|
+
|
|
72
|
+
This example demonstrates how to handle file uploads in multipart form data.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from multipart_decoder import MultipartDecoder
|
|
76
|
+
import os
|
|
77
|
+
|
|
78
|
+
def process_file_upload():
|
|
79
|
+
"""Process an example multipart form with file uploads."""
|
|
80
|
+
content_type = (
|
|
81
|
+
"multipart/form-data; boundary=----WebKitFormBoundary4AQwYOUUCEUw9hsX"
|
|
82
|
+
)
|
|
83
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnk0QVF3WU9VVUNFVXc5aHNYLS0NCg=="
|
|
84
|
+
|
|
85
|
+
form_data = MultipartDecoder(body, content_type)
|
|
86
|
+
# Process regular form field
|
|
87
|
+
print(f"Field 1: {form_data['field1']}")
|
|
88
|
+
print(f"Field 2: {form_data['field2']}")
|
|
89
|
+
|
|
90
|
+
# Process file uploads
|
|
91
|
+
if 'file1' in form_data:
|
|
92
|
+
filename, content_type, file_data = form_data['file1']
|
|
93
|
+
print(f" - Filename: {filename}")
|
|
94
|
+
print(f" - Content type: {content_type}")
|
|
95
|
+
print(f" - File size: {len(file_data)} bytes")
|
|
96
|
+
|
|
97
|
+
# Example: save the file
|
|
98
|
+
save_path = f"saved_{filename}"
|
|
99
|
+
with open(save_path, "wb") as f:
|
|
100
|
+
f.write(file_data)
|
|
101
|
+
print(f" - File saved to: {save_path}")
|
|
102
|
+
|
|
103
|
+
# # After using the file, delete it for this example
|
|
104
|
+
os.remove(save_path)
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
process_file_upload()
|
|
108
|
+
```
|
|
109
|
+
# Expected Output:
|
|
110
|
+
|
|
111
|
+
### Test Fields:
|
|
112
|
+
- **Field 1**: `value1`
|
|
113
|
+
- **Field 2**: `value2`
|
|
114
|
+
|
|
115
|
+
### File Field:
|
|
116
|
+
- **Filename**: `example.txt`
|
|
117
|
+
- **Content Type**: `text/plain`
|
|
118
|
+
- **File Size**: `13 bytes`
|
|
119
|
+
- **File Saved To**: `saved_example.txt`
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/multipart_decoder/__init__.py
|
|
5
|
+
src/multipart_decoder/decoder.py
|
|
6
|
+
src/multipart_decoder.egg-info/PKG-INFO
|
|
7
|
+
src/multipart_decoder.egg-info/SOURCES.txt
|
|
8
|
+
src/multipart_decoder.egg-info/dependency_links.txt
|
|
9
|
+
src/multipart_decoder.egg-info/top_level.txt
|
|
10
|
+
tests/test_decoder.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
multipart_decoder
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Unit tests for the multipart_decoder package.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import unittest
|
|
6
|
+
from multipart_decoder import MultipartDecoder
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TestMultipartDecoder(unittest.TestCase):
|
|
10
|
+
def test_basic_form_fields(self):
|
|
11
|
+
"""Test decoding of basic form fields."""
|
|
12
|
+
content_type = (
|
|
13
|
+
"multipart/form-data; boundary=----WebKitFormBoundaryp0mBfJEEcQtmxFCF"
|
|
14
|
+
)
|
|
15
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cDBtQmZKRUVjUXRteEZDRg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cDBtQmZKRUVjUXRteEZDRg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cDBtQmZKRUVjUXRteEZDRi0tDQo="
|
|
16
|
+
|
|
17
|
+
result = MultipartDecoder(body, content_type)
|
|
18
|
+
self.assertEqual(result["field1"], "value1")
|
|
19
|
+
self.assertEqual(result["field2"], "value2")
|
|
20
|
+
|
|
21
|
+
def test_file_upload(self):
|
|
22
|
+
"""Test decoding of file uploads."""
|
|
23
|
+
content_type = (
|
|
24
|
+
"multipart/form-data; boundary=----WebKitFormBoundaryI6YbOCiSAMjU6wK2"
|
|
25
|
+
)
|
|
26
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5STZZYk9DaVNBTWpVNndLMg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlJNlliT0NpU0FNalU2d0syLS0NCg=="
|
|
27
|
+
|
|
28
|
+
result = MultipartDecoder(body, content_type)
|
|
29
|
+
self.assertTrue("file1" in result)
|
|
30
|
+
self.assertTrue(isinstance(result["file1"], tuple))
|
|
31
|
+
|
|
32
|
+
filename, content_type, data = result["file1"]
|
|
33
|
+
self.assertEqual(filename, "example.txt")
|
|
34
|
+
self.assertEqual(content_type, "text/plain")
|
|
35
|
+
self.assertEqual(data, b"Hello, world!")
|
|
36
|
+
|
|
37
|
+
def test_mixed_content(self):
|
|
38
|
+
"""Test decoding of mixed content (form fields and file uploads)."""
|
|
39
|
+
content_type = (
|
|
40
|
+
"multipart/form-data; boundary=----WebKitFormBoundary4AQwYOUUCEUw9hsX"
|
|
41
|
+
)
|
|
42
|
+
body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnk0QVF3WU9VVUNFVXc5aHNYLS0NCg=="
|
|
43
|
+
|
|
44
|
+
result = MultipartDecoder(body, content_type)
|
|
45
|
+
self.assertEqual(result["field1"], "value1")
|
|
46
|
+
|
|
47
|
+
filename, content_type, data = result["file1"]
|
|
48
|
+
self.assertEqual(filename, "example.txt")
|
|
49
|
+
self.assertEqual(content_type, "text/plain")
|
|
50
|
+
self.assertEqual(data, b"Hello, world!")
|
|
51
|
+
|
|
52
|
+
def test_invalid_content(self):
|
|
53
|
+
"""Test handling of invalid content."""
|
|
54
|
+
content_type = (
|
|
55
|
+
"multipart/form-data; boundary=----WebKitFormBoundaryI6YbOCiSAMjU6wK2"
|
|
56
|
+
)
|
|
57
|
+
body = "LS0tWxsbCg=="
|
|
58
|
+
|
|
59
|
+
with self.assertRaises(ValueError):
|
|
60
|
+
MultipartDecoder(body, content_type)
|
|
61
|
+
|
|
62
|
+
def test_missing_boundary(self):
|
|
63
|
+
"""Test handling of missing boundary parameter."""
|
|
64
|
+
content_type = "multipart/form-data"
|
|
65
|
+
body = "LS0tWxsbCg=="
|
|
66
|
+
|
|
67
|
+
with self.assertRaises(ValueError):
|
|
68
|
+
MultipartDecoder(body, content_type)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == "__main__":
|
|
72
|
+
unittest.main()
|