0
# WSGI Request Processing
1
2
WSGI utilities for converting Lambda events to standard HTTP requests and middleware for AWS-specific processing requirements. These functions enable seamless integration of existing WSGI applications with AWS Lambda and API Gateway.
3
4
## Capabilities
5
6
### Event to WSGI Conversion
7
8
Convert AWS Lambda events to WSGI environ dictionaries for processing by web applications.
9
10
```python { .api }
11
def create_wsgi_request(
12
event,
13
script_name=None,
14
base_path=None,
15
trailing_slash=True
16
):
17
"""
18
Create WSGI environ dictionary from Lambda event.
19
20
Converts API Gateway Lambda proxy integration event into
21
standard WSGI environ dict for web application processing.
22
23
Parameters:
24
- event: dict, API Gateway Lambda proxy event
25
- script_name: str, WSGI SCRIPT_NAME value
26
- base_path: str, base path for the application
27
- trailing_slash: bool, add trailing slash to paths
28
29
Returns:
30
dict: WSGI environ dictionary
31
"""
32
```
33
34
### Payload Processing
35
36
Process different API Gateway payload formats.
37
38
```python { .api }
39
def process_lambda_payload_v1(event):
40
"""
41
Process API Gateway v1.0 payload format.
42
43
Extracts HTTP request data from API Gateway v1.0 Lambda
44
proxy integration event format.
45
46
Parameters:
47
- event: dict, API Gateway v1.0 event
48
49
Returns:
50
tuple: (method, path, headers, body, query_params)
51
"""
52
```
53
54
```python { .api }
55
def process_lambda_payload_v2(event):
56
"""
57
Process API Gateway v2.0 payload format.
58
59
Extracts HTTP request data from API Gateway v2.0 Lambda
60
proxy integration event format.
61
62
Parameters:
63
- event: dict, API Gateway v2.0 event
64
65
Returns:
66
tuple: (method, path, headers, body, query_params)
67
"""
68
```
69
70
### Response Formatting
71
72
Format responses for API Gateway integration.
73
74
```python { .api }
75
def common_log(environ, response, response_time=None):
76
"""
77
Create Apache Common Log Format entries.
78
79
Generates standardized log entries from WSGI environ
80
and response data for request monitoring.
81
82
Parameters:
83
- environ: dict, WSGI environ dictionary
84
- response: dict, HTTP response data
85
- response_time: float, request processing time in seconds
86
87
Returns:
88
str: Common Log Format entry
89
"""
90
```
91
92
```python { .api }
93
def get_wsgi_string(wsgi_string):
94
"""
95
Encode string for WSGI compatibility.
96
97
Ensures proper string encoding for WSGI environ values
98
and HTTP headers.
99
100
Parameters:
101
- wsgi_string: str, input string to encode
102
103
Returns:
104
str: WSGI-compatible encoded string
105
"""
106
```
107
108
## WSGI Middleware
109
110
### ZappaWSGIMiddleware
111
112
WSGI middleware providing Zappa-specific request handling and AWS compatibility.
113
114
```python { .api }
115
class ZappaWSGIMiddleware:
116
"""
117
WSGI middleware for Zappa-specific request handling.
118
119
Provides WSGI application interface with Zappa-specific
120
processing for Lambda/API Gateway integration including
121
header manipulation for AWS compatibility.
122
"""
123
124
def __init__(self, application):
125
"""
126
Initialize middleware with WSGI application.
127
128
Parameters:
129
- application: callable, WSGI application
130
"""
131
132
def __call__(self, environ, start_response):
133
"""
134
WSGI application interface.
135
136
Processes WSGI requests with Zappa-specific handling
137
and delegates to wrapped application.
138
139
Parameters:
140
- environ: dict, WSGI environ dictionary
141
- start_response: callable, WSGI start_response function
142
143
Returns:
144
iterable: WSGI response
145
"""
146
```
147
148
### Utility Functions
149
150
```python { .api }
151
def all_casings(input_text):
152
"""
153
Generate all case permutations of a string.
154
155
Used for case-insensitive header matching in AWS
156
Lambda/API Gateway environments.
157
158
Parameters:
159
- input_text: str, text to generate casings for
160
161
Returns:
162
list: All possible case permutations
163
"""
164
```
165
166
## Constants
167
168
```python { .api }
169
# HTTP methods that may contain binary data
170
BINARY_METHODS = ['POST', 'PUT', 'PATCH']
171
```
172
173
## Usage Examples
174
175
### Basic WSGI Conversion
176
177
```python
178
from zappa.wsgi import create_wsgi_request
179
180
def lambda_handler(event, context):
181
# Convert Lambda event to WSGI environ
182
environ = create_wsgi_request(event)
183
184
# Use with existing WSGI application
185
response = my_wsgi_app(environ, start_response)
186
return response
187
```
188
189
### Django Integration
190
191
```python
192
from zappa.wsgi import create_wsgi_request, ZappaWSGIMiddleware
193
from django.core.wsgi import get_wsgi_application
194
195
# Wrap Django WSGI app with Zappa middleware
196
django_app = get_wsgi_application()
197
zappa_app = ZappaWSGIMiddleware(django_app)
198
199
def lambda_handler(event, context):
200
environ = create_wsgi_request(event)
201
return zappa_app(environ, start_response)
202
```
203
204
### Flask Integration
205
206
```python
207
from zappa.wsgi import create_wsgi_request, ZappaWSGIMiddleware
208
from flask import Flask
209
210
app = Flask(__name__)
211
zappa_app = ZappaWSGIMiddleware(app)
212
213
@app.route('/')
214
def hello():
215
return 'Hello from Lambda!'
216
217
def lambda_handler(event, context):
218
environ = create_wsgi_request(event)
219
return zappa_app(environ, start_response)
220
```
221
222
### Custom Response Processing
223
224
```python
225
from zappa.wsgi import common_log, process_lambda_payload_v1
226
import time
227
228
def lambda_handler(event, context):
229
start_time = time.time()
230
231
# Process API Gateway payload
232
method, path, headers, body, query_params = process_lambda_payload_v1(event)
233
234
# Create WSGI environ
235
environ = create_wsgi_request(event)
236
237
# Process request
238
response = my_app(environ, start_response)
239
240
# Log request
241
response_time = time.time() - start_time
242
log_entry = common_log(environ, response, response_time)
243
print(log_entry)
244
245
return response
246
```
247
248
### Binary Content Handling
249
250
```python
251
from zappa.wsgi import BINARY_METHODS, get_wsgi_string
252
import base64
253
254
def lambda_handler(event, context):
255
environ = create_wsgi_request(event)
256
257
# Handle binary content for specific methods
258
if environ['REQUEST_METHOD'] in BINARY_METHODS:
259
if event.get('isBase64Encoded', False):
260
body = base64.b64decode(event['body'])
261
environ['wsgi.input'] = io.BytesIO(body)
262
263
return my_app(environ, start_response)
264
```