History

http: Factorize the code by adding http_read_header()

This function is used for reading http reply headers.

Samuel Pitoiset committed 14 years ago

Commit 5d603f11 parent 4f04f5c1 file · +40 −38Browse files

Unified · Split

libavformat/http.c+40−38
@@ -324,13 +324,35 @@ static inline int has_header(const char *str, const char *header) · 323 unmodified lines
324324 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
325325 }
326326
327+static int http_read_header(URLContext *h, int *new_location)
328+{
329+ HTTPContext *s = h->priv_data;
330+ char line[1024];
331+ int err = 0;
332+
333+ for (;;) {
334+ if (http_get_line(s, line, sizeof(line)) < 0)
335+ return AVERROR(EIO);
336+
337+ av_dlog(NULL, "header='%s'\n", line);
338+
339+ err = process_line(h, line, s->line_count, new_location);
340+ if (err < 0)
341+ return err;
342+ if (err == 0)
343+ break;
344+ s->line_count++;
345+ }
346+
347+ return err;
348+}
349+
327350 static int http_connect(URLContext *h, const char *path, const char *local_path,
328351 const char *hoststr, const char *auth,
329352 const char *proxyauth, int *new_location)
330353 {
331354 HTTPContext *s = h->priv_data;
332355 int post, err;
333− char line[1024];
334356 char headers[1024] = "";
335357 char *authstr = NULL, *proxyauthstr = NULL;
336358 int64_t off = s->off;
@@ -403,19 +425,9 @@ static int http_connect(URLContext *h, const char *path, const char *local_path, · 66 unmodified lines
403425 s->chunksize = -1;
404426
405427 /* wait for header */
406− for(;;) {
407− if (http_get_line(s, line, sizeof(line)) < 0)
408− return AVERROR(EIO);
409−
410− av_dlog(NULL, "header='%s'\n", line);
411−
412− err = process_line(h, line, s->line_count, new_location);
413− if (err < 0)
414− return err;
415− if (err == 0)
416− break;
417− s->line_count++;
418− }
428+ err = http_read_header(h, new_location);
429+ if (err < 0)
430+ return err;
419431
420432 return (off == s->off) ? 0 : -1;
421433 }
@@ -603,10 +615,11 @@ static int http_proxy_open(URLContext *h, const char *uri, int flags) · 181 unmodified lines
603615 HTTPContext *s = h->priv_data;
604616 char hostname[1024], hoststr[1024];
605617 char auth[1024], pathbuf[1024], *path;
606− char line[1024], lower_url[100];
618+ char lower_url[100];
607619 int port, ret = 0, attempts = 0;
608620 HTTPAuthType cur_auth_type;
609621 char *authstr;
622+ int new_loc;
610623
611624 h->is_streamed = 1;
612625
@@ -647,30 +660,19 @@ redo: · 34 unmodified lines
647660 s->filesize = -1;
648661 cur_auth_type = s->proxy_auth_state.auth_type;
649662
650− for (;;) {
651− int new_loc;
652− // Note: This uses buffering, potentially reading more than the
653− // HTTP header. If tunneling a protocol where the server starts
654− // the conversation, we might buffer part of that here, too.
655− // Reading that requires using the proper ffurl_read() function
656− // on this URLContext, not using the fd directly (as the tls
657− // protocol does). This shouldn't be an issue for tls though,
658− // since the client starts the conversation there, so there
659− // is no extra data that we might buffer up here.
660− if (http_get_line(s, line, sizeof(line)) < 0) {
661− ret = AVERROR(EIO);
662− goto fail;
663− }
664−
665− av_dlog(h, "header='%s'\n", line);
663+ /* Note: This uses buffering, potentially reading more than the
664+ * HTTP header. If tunneling a protocol where the server starts
665+ * the conversation, we might buffer part of that here, too.
666+ * Reading that requires using the proper ffurl_read() function
667+ * on this URLContext, not using the fd directly (as the tls
668+ * protocol does). This shouldn't be an issue for tls though,
669+ * since the client starts the conversation there, so there
670+ * is no extra data that we might buffer up here.
671+ */
672+ ret = http_read_header(h, &new_loc);
673+ if (ret < 0)
674+ goto fail;
666675
667− ret = process_line(h, line, s->line_count, &new_loc);
668− if (ret < 0)
669− goto fail;
670− if (ret == 0)
671− break;
672− s->line_count++;
673− }
674676 attempts++;
675677 if (s->http_code == 407 &&
676678 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t