瀏覽代碼

[http] Support HTTP redirection

tags/v0.9.8
Michael Brown 15 年之前
父節點
當前提交
272e6ddc30
共有 1 個文件被更改,包括 28 次插入0 次删除
  1. 28
    0
      src/net/tcp/http.c

+ 28
- 0
src/net/tcp/http.c 查看文件

@@ -138,6 +138,8 @@ static void http_done ( struct http_request *http, int rc ) {
138 138
 static int http_response_to_rc ( unsigned int response ) {
139 139
 	switch ( response ) {
140 140
 	case 200:
141
+	case 301:
142
+	case 302:
141 143
 		return 0;
142 144
 	case 404:
143 145
 		return -ENOENT;
@@ -180,6 +182,28 @@ static int http_rx_response ( struct http_request *http, char *response ) {
180 182
 	return 0;
181 183
 }
182 184
 
185
+/**
186
+ * Handle HTTP Location header
187
+ *
188
+ * @v http		HTTP request
189
+ * @v value		HTTP header value
190
+ * @ret rc		Return status code
191
+ */
192
+static int http_rx_location ( struct http_request *http, const char *value ) {
193
+	int rc;
194
+
195
+	/* Redirect to new location */
196
+	DBGC ( http, "HTTP %p redirecting to %s\n", http, value );
197
+	if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI_STRING,
198
+				    value ) ) != 0 ) {
199
+		DBGC ( http, "HTTP %p could not redirect: %s\n",
200
+		       http, strerror ( rc ) );
201
+		return rc;
202
+	}
203
+
204
+	return 0;
205
+}
206
+
183 207
 /**
184 208
  * Handle HTTP Content-Length header
185 209
  *
@@ -222,6 +246,10 @@ struct http_header_handler {
222 246
 
223 247
 /** List of HTTP header handlers */
224 248
 static struct http_header_handler http_header_handlers[] = {
249
+	{
250
+		.header = "Location",
251
+		.rx = http_rx_location,
252
+	},
225 253
 	{
226 254
 		.header = "Content-Length",
227 255
 		.rx = http_rx_content_length,

Loading…
取消
儲存