comparison http.c @ 6153:2e07dcbab954 libavformat

HTTP: Get rid of the is_chunked variable, use the chunksize variable instead
author mstorsjo
date Mon, 21 Jun 2010 19:01:32 +0000
parents b9dee5077174
children 78f96a0ef08c
comparison
equal deleted inserted replaced
6152:b9dee5077174 6153:2e07dcbab954
46 int64_t off, filesize; 46 int64_t off, filesize;
47 char location[URL_SIZE]; 47 char location[URL_SIZE];
48 HTTPAuthState auth_state; 48 HTTPAuthState auth_state;
49 int init; 49 int init;
50 unsigned char headers[BUFFER_SIZE]; 50 unsigned char headers[BUFFER_SIZE];
51 int is_chunked;
52 } HTTPContext; 51 } HTTPContext;
53 52
54 static int http_connect(URLContext *h, const char *path, const char *hoststr, 53 static int http_connect(URLContext *h, const char *path, const char *hoststr,
55 const char *auth, int *new_location); 54 const char *auth, int *new_location);
56 55
65 av_strlcpy(s->headers, headers, sizeof(s->headers)); 64 av_strlcpy(s->headers, headers, sizeof(s->headers));
66 } 65 }
67 66
68 void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked) 67 void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked)
69 { 68 {
70 ((HTTPContext*)h->priv_data)->is_chunked = is_chunked; 69 ((HTTPContext*)h->priv_data)->chunksize = is_chunked ? 0 : -1;
71 } 70 }
72 71
73 /* return non zero if error */ 72 /* return non zero if error */
74 static int http_open_cnx(URLContext *h) 73 static int http_open_cnx(URLContext *h)
75 { 74 {
150 if (!s) { 149 if (!s) {
151 return AVERROR(ENOMEM); 150 return AVERROR(ENOMEM);
152 } 151 }
153 h->priv_data = s; 152 h->priv_data = s;
154 s->filesize = -1; 153 s->filesize = -1;
155 s->is_chunked = 1; 154 s->chunksize = 0; /* Default to chunked POSTs */
156 s->off = 0; 155 s->off = 0;
157 s->init = 0; 156 s->init = 0;
158 s->hd = NULL; 157 s->hd = NULL;
159 *s->headers = '\0'; 158 *s->headers = '\0';
160 memset(&s->auth_state, 0, sizeof(s->auth_state)); 159 memset(&s->auth_state, 0, sizeof(s->auth_state));
314 "%s" 313 "%s"
315 "%s" 314 "%s"
316 "\r\n", 315 "\r\n",
317 post ? "POST" : "GET", 316 post ? "POST" : "GET",
318 path, 317 path,
319 post && s->is_chunked ? "Transfer-Encoding: chunked\r\n" : "", 318 post && s->chunksize >= 0 ? "Transfer-Encoding: chunked\r\n" : "",
320 headers, 319 headers,
321 authstr ? authstr : ""); 320 authstr ? authstr : "");
322 321
323 av_freep(&authstr); 322 av_freep(&authstr);
324 if (url_write(s->hd, s->buffer, strlen(s->buffer)) < 0) 323 if (url_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
328 s->buf_ptr = s->buffer; 327 s->buf_ptr = s->buffer;
329 s->buf_end = s->buffer; 328 s->buf_end = s->buffer;
330 s->line_count = 0; 329 s->line_count = 0;
331 s->off = 0; 330 s->off = 0;
332 s->filesize = -1; 331 s->filesize = -1;
333 s->chunksize = -1;
334 if (post) { 332 if (post) {
335 /* always use chunked encoding for upload data */
336 s->chunksize = 0;
337 /* Pretend that it did work. We didn't read any header yet, since 333 /* Pretend that it did work. We didn't read any header yet, since
338 * we've still to send the POST data, but the code calling this 334 * we've still to send the POST data, but the code calling this
339 * function will check http_code after we return. */ 335 * function will check http_code after we return. */
340 s->http_code = 200; 336 s->http_code = 200;
341 return 0; 337 return 0;
342 } 338 }
339 s->chunksize = -1;
343 340
344 /* wait for header */ 341 /* wait for header */
345 for(;;) { 342 for(;;) {
346 if (http_get_line(s, line, sizeof(line)) < 0) 343 if (http_get_line(s, line, sizeof(line)) < 0)
347 return AVERROR(EIO); 344 return AVERROR(EIO);
440 437
441 /* silently ignore zero-size data since chunk encoding that would 438 /* silently ignore zero-size data since chunk encoding that would
442 * signal EOF */ 439 * signal EOF */
443 if (size > 0) { 440 if (size > 0) {
444 /* upload data using chunked encoding */ 441 /* upload data using chunked encoding */
445 if(s->is_chunked) {
446 snprintf(temp, sizeof(temp), "%x\r\n", size); 442 snprintf(temp, sizeof(temp), "%x\r\n", size);
447 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0) 443 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0)
448 return ret; 444 return ret;
449 }
450 445
451 if ((ret = url_write(s->hd, buf, size)) < 0) 446 if ((ret = url_write(s->hd, buf, size)) < 0)
452 return ret; 447 return ret;
453 448
454 if (s->is_chunked && (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0) 449 if ((ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
455 return ret; 450 return ret;
456 } 451 }
457 return size; 452 return size;
458 } 453 }
459 454