static int http_get_line(HTTPContext *s, char *line, int line_size) { int ch; char *q; q = line; for(;;) { ch = http_getc(s); if (ch < 0) return AVERROR(EIO); if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; return 0; } else { if ((q - line) < line_size - 1) *q++ = ch; } } }
static int http_connect(URLContext *h, const char *path, const char *hoststr, const char *auth, int *new_location) { HTTPContext *s = h->priv_data; int post, err, ch; char line[1024], *q; char *auth_b64; int auth_b64_len = strlen(auth)* 4 / 3 + 12; int64_t off = s->off; /* send http header */ post = h->flags & URL_WRONLY; auth_b64 = av_malloc(auth_b64_len); av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth)); snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.1\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Range: bytes=%"PRId64"-\r\n" "Host: %s\r\n" "Authorization: Basic %s\r\n" "Connection: close\r\n" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, s->off, hoststr, auth_b64); av_freep(&auth_b64); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR(EIO); /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->off = 0; s->filesize = -1; if (post) { return 0; } /* wait for header */ q = line; for(;;) { ch = http_getc(s); if (ch < 0) return AVERROR(EIO); if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG printf("header='%s'\n", line); #endif err = process_line(h, line, s->line_count, new_location); if (err < 0) return err; if (err == 0) break; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } return (off == s->off) ? 0 : -1; }
static int http_connect(HTTPContext *s, const char *path, const char *hoststr, int flags, int wait) { int post, err, ch; char line[512], *q; hdr_clear(s); /* send http header */ post = flags & O_WRONLY; s->len = 0; s->tread = 0; s->bpos = 0; s->bsz = 0; snprintf(line, sizeof(line), "%s %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Host: %s\r\n" "Accept: */*\r\n" "Connection: keep-alive\r\n" "\r\n", post ? "POST" : "GET", path, //"Wget/1.9", "dcplaya_net", hoststr); if (http_write((uint32) s, line, strlen(line)) < 0) return -1; #ifdef DEBUG printf("http : sent header -->\n%s", line); #endif /* init input buffer */ s->line_count = 0; //s->location[0] = '\0'; if (post) { //sleep(1); return 0; } /* wait for header */ q = line; if (wait) thd_sleep(wait); for(;;) { ch = http_getc((uint32) s); #ifdef DEBUG //printf("%c", ch); #endif if (ch < 0) { printf("http header truncated\n"); return 0; } if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG printf("header='%s'\n", line); #endif if (line[0]) hdr_add(s, line); err = process_line(s, line, s->line_count); if (err < 0) return err; if (err == 0) return 0; //return s->len? 0 : -1; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } }
static int http_connect(URLContext *h, const char *path, const char *hoststr, const char *auth) { HTTPContext *s = h->priv_data; int post, err, ch; char line[1024], *q; char *auth_b64; /* send http header */ post = h->flags & URL_WRONLY; auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth)); snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Host: %s\r\n" "Authorization: Basic %s\r\n" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, hoststr, auth_b64); av_freep(&auth_b64); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR_IO; /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->location[0] = '\0'; if (post) { sleep(1); return 0; } /* wait for header */ q = line; for(;;) { ch = http_getc(s); if (ch < 0) return AVERROR_IO; if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG printf("header='%s'\n", line); #endif err = process_line(s, line, s->line_count); if (err < 0) return err; if (err == 0) return 0; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } }