// pong_thread(threadarg) // Connect to the server at the position indicated by `threadarg` // (which is a pointer to a `pong_args` structure). void* pong_thread(void* threadarg) { pthread_detach(pthread_self()); // Copy thread arguments onto our stack. pong_args pa = *((pong_args*) threadarg); char url[256]; snprintf(url, sizeof(url), "move?x=%d&y=%d&style=on", pa.x, pa.y); http_connection* conn = http_connect(pong_addr); http_send_request(conn, url); http_receive_response_headers(conn); if (conn->status_code != 200) fprintf(stderr, "%.3f sec: warning: %d,%d: " "server returned status %d (expected 200)\n", elapsed(), pa.x, pa.y, conn->status_code); http_receive_response_body(conn); double result = strtod(conn->buf, NULL); if (result < 0) { fprintf(stderr, "%.3f sec: server returned error: %s\n", elapsed(), http_truncate_response(conn)); exit(1); } http_close(conn); // signal the main thread to continue pthread_cond_signal(&condvar); // and exit! pthread_exit(NULL); }
struct http_session* http_session_open(const char* url) { int peer_handle = 0; struct sockaddr_in server; char *request, host_addr[32]; struct http_session* session; session = (struct http_session*) rt_malloc(sizeof(struct http_session)); if(session == RT_NULL) return RT_NULL; session->size = 0; session->position = 0; /* Check valid IP address and URL */ if(http_resolve_address(&server, url, &host_addr[0], &request) != 0) { rt_free(session); return RT_NULL; } // Now we connect and initiate the transfer by sending a // request header to the server, and receiving the response header if((peer_handle = http_connect(session, &server, host_addr, request)) < 0) { rt_kprintf("HTTP: failed to connect to '%s'!\n", host_addr); rt_free(session); return RT_NULL; } // http connect returns valid socket. Save in handle list. session->socket = peer_handle; /* open successfully */ return session; }
int httpreq(char** buffer, size_t* length) { int socket_fd; size_t request_text_length; if ((socket_fd = http_connect(LFM_HOST)) < 0) { LOG(LOG_CRITICAL, "Could not connect to %s.", LFM_HOST); exit(ERROR); } http_request(NEW_RELEASE_URL, strlen(NEW_RELEASE_URL), LFM_HOST, strlen(LFM_HOST), buffer, &request_text_length); if (http_send_request(socket_fd, *buffer, request_text_length) == -1) { LOG(LOG_CRITICAL, "Could not send request."); exit(ERROR); } free(*buffer); if (http_read_response(socket_fd, buffer, length)) { LOG(LOG_CRITICAL, "Could not read request"); exit(ERROR); } http_close(socket_fd); return OK; }
int https_connect(struct url *url, struct url *proxy) { static struct tls_config *tls_config = NULL; int s; /* One time initialization */ if (tls_config == NULL) tls_config = https_init(); if ((ctx = tls_client()) == NULL) { warnx("failed to create tls client"); return -1; } if (tls_configure(ctx, tls_config) != 0) { warnx("%s: %s", __func__, tls_error(ctx)); return -1; } if (url->port[0] == '\0') (void)strlcpy(url->port, "443", sizeof(url->port)); if ((s = http_connect(url, proxy)) == -1) return -1; if (tls_connect_socket(ctx, s, url->host) != 0) { warnx("%s: %s", __func__, tls_error(ctx)); return -1; } return s; }
static int http_open_cnx_internal(URLContext *h, AVDictionary **options) { const char *path, *proxy_path, *lower_proto = "tcp", *local_path; char hostname[1024], hoststr[1024], proto[10]; char auth[1024], proxyauth[1024] = ""; char path1[MAX_URL_SIZE]; char buf[1024], urlbuf[MAX_URL_SIZE]; int port, use_proxy, err, location_changed = 0; HTTPContext *s = h->priv_data; av_url_split(proto, sizeof(proto), auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); proxy_path = getenv("http_proxy"); use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) && proxy_path && av_strstart(proxy_path, "http://", NULL); if (!strcmp(proto, "https")) { lower_proto = "tls"; use_proxy = 0; if (port < 0) port = 443; } if (port < 0) port = 80; if (path1[0] == '\0') path = "/"; else path = path1; local_path = path; if (use_proxy) { /* Reassemble the request URL without auth string - we don't * want to leak the auth to the proxy. */ ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s", path1); path = urlbuf; av_url_split(NULL, 0, proxyauth, sizeof(proxyauth), hostname, sizeof(hostname), &port, NULL, 0, proxy_path); } ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL); if (!s->hd) { err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE, &h->interrupt_callback, options); if (err < 0) return err; } err = http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed); if (err < 0) return err; return location_changed; }
int conn_init( conn_t *conn ) { char *proxy = conn->conf->http_proxy, *host = conn->conf->no_proxy; int i; if( *conn->conf->http_proxy == 0 ) { proxy = NULL; } else if( *conn->conf->no_proxy != 0 ) { for( i = 0; ; i ++ ) if( conn->conf->no_proxy[i] == 0 ) { if( strstr( conn->host, host ) != NULL ) proxy = NULL; host = &conn->conf->no_proxy[i+1]; if( conn->conf->no_proxy[i+1] == 0 ) break; } } conn->proxy = proxy != NULL; if( conn->proto == PROTO_FTP && !conn->proxy ) { conn->ftp->local_if = conn->local_if; conn->ftp->ftp_mode = FTP_PASSIVE; if( !ftp_connect( conn->ftp, conn->host, conn->port, conn->user, conn->pass ) ) { conn->message = conn->ftp->message; conn_disconnect( conn ); return( 0 ); } conn->message = conn->ftp->message; if( !ftp_cwd( conn->ftp, conn->dir ) ) { conn_disconnect( conn ); return( 0 ); } } else { conn->http->local_if = conn->local_if; if( !http_connect( conn->http, conn->proto, proxy, conn->host, conn->port, conn->user, conn->pass ) ) { conn->message = conn->http->headers; conn_disconnect( conn ); return( 0 ); } conn->message = conn->http->headers; conn->fd = conn->http->fd; } return( 1 ); }
static off_t http_seek(uint32 hnd, off_t offset, int whence) { HTTPContext *s = (HTTPContext *)hnd; file_t hd; //printf("http_seek %d %d\n", offset, whence); if (whence == SEEK_SET) { whence = SEEK_CUR; offset = offset - s->tread; } if (whence == SEEK_CUR) { int pos = fs_seek(s->hd, 0, SEEK_CUR); int res = fs_seek(s->hd, offset, SEEK_CUR) - pos; //printf("http_seek res = %d\n", res); s->tread += res; return s->tread; } if (whence != SEEK_SET) return -1; fs_close(s->hd); s->hd = -1; //printf("location = '%s'\n", s->location); hd = fs_open(s->location, O_RDWR); if (hd<0) { printf("http_seek : could not reconnect to '%s'\n", s->location); goto fail; } s->hd = hd; if (http_connect(s, s->path, s->hoststr, s->flags, 0) < 0) goto fail; if (offset) { int pos = fs_seek(s->hd, 0, SEEK_CUR); s->tread = fs_seek(s->hd, offset, SEEK_CUR) - pos; //printf("http_seek res = %d\n", s->tread); return s->tread; } s->tread = 0; return 0; fail: printf("http_seek : failed reopen\n"); if (s->hd >= 0) fs_close(s->hd); s->hd = 0; return -1; }
static void http_base_test(void) { struct bufferevent *bev; int fd; const char *http_request; short port = -1; test_ok = 0; fprintf(stdout, "Testing HTTP Server Event Base: "); base = event_init(); /* * create another bogus base - which is being used by all subsequen * tests - yuck! */ event_init(); http = http_setup(&port, base); fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); bufferevent_base_set(base, bev); http_request = "GET /test HTTP/1.1\r\n" "Host: somehost\r\n" "Connection: close\r\n" "\r\n"; bufferevent_write(bev, http_request, strlen(http_request)); event_base_dispatch(base); bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); evhttp_free(http); event_base_free(base); base = NULL; if (test_ok != 2) { fprintf(stdout, "FAILED\n"); exit(1); } fprintf(stdout, "OK\n"); }
int main(void) { http_ctx_t ctx; if(false == http_connect(&ctx, "http://board.raidrush.ws/forumdisplay.php")) { printf("unable to connect to remote host.\r\n"); return -1; } http_option_set(&ctx, LIBNET_HTTP_OPT_PARAM, "f=13"); http_execute(&ctx); http_disconnect(&ctx); printf("\r\nLast Error: %s (%d)\n", libnet_str_error(libnet_error_get()), libnet_error_get()); return 0; }
static void http_multi_line_header_test(void) { struct bufferevent *bev; int fd; const char *http_start_request; short port = -1; test_ok = 0; fprintf(stdout, "Testing HTTP Server with multi line: "); http = http_setup(&port, NULL); fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); http_start_request = "GET /test HTTP/1.1\r\n" "Host: somehost\r\n" "Connection: close\r\n" "X-Multi: aaaaaaaa\r\n" " a\r\n" "\tEND\r\n" "X-Last: last\r\n" "\r\n"; bufferevent_write(bev, http_start_request, strlen(http_start_request)); event_dispatch(); bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); evhttp_free(http); if (test_ok != 4) { fprintf(stdout, "FAILED\n"); exit(1); } fprintf(stdout, "OK\n"); }
/* * Testing that the HTTP server can deal with a malformed request. */ static void http_failure_test(void) { struct bufferevent *bev; int fd; const char *http_request; short port = -1; test_ok = 0; fprintf(stdout, "Testing Bad HTTP Request: "); http = http_setup(&port, NULL); fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_failure_readcb, http_writecb, http_errorcb, NULL); http_request = "illegal request\r\n"; bufferevent_write(bev, http_request, strlen(http_request)); event_dispatch(); bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); evhttp_free(http); if (test_ok != 2) { fprintf(stdout, "FAILED\n"); exit(1); } fprintf(stdout, "OK\n"); }
static int http_action( git_smart_subtransport_stream **stream, git_smart_subtransport *subtransport, const char *url, git_smart_service_t action) { http_subtransport *t = (http_subtransport *)subtransport; int ret; if (!stream) return -1; if ((!t->connection_data.host || !t->connection_data.port || !t->connection_data.path) && (ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0) return ret; if (http_connect(t) < 0) return -1; switch (action) { case GIT_SERVICE_UPLOADPACK_LS: return http_uploadpack_ls(t, stream); case GIT_SERVICE_UPLOADPACK: return http_uploadpack(t, stream); case GIT_SERVICE_RECEIVEPACK_LS: return http_receivepack_ls(t, stream); case GIT_SERVICE_RECEIVEPACK: return http_receivepack(t, stream); } *stream = NULL; return -1; }
static int http_stream_read( git_smart_subtransport_stream *stream, char *buffer, size_t buf_size, size_t *bytes_read) { http_stream *s = (http_stream *)stream; http_subtransport *t = OWNING_SUBTRANSPORT(s); parser_context ctx; size_t bytes_parsed; replay: *bytes_read = 0; assert(t->connected); if (!s->sent_request) { git_buf request = GIT_BUF_INIT; clear_parser_state(t); if (gen_request(&request, s, 0) < 0) { giterr_set(GITERR_NET, "Failed to generate request"); return -1; } if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) { git_buf_free(&request); return -1; } git_buf_free(&request); s->sent_request = 1; } if (!s->received_response) { if (s->chunked) { assert(s->verb == post_verb); /* Flush, if necessary */ if (s->chunk_buffer_len > 0 && write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0) return -1; s->chunk_buffer_len = 0; /* Write the final chunk. */ if (gitno_send(&t->socket, "0\r\n\r\n", 5, 0) < 0) return -1; } s->received_response = 1; } while (!*bytes_read && !t->parse_finished) { t->parse_buffer.offset = 0; if (gitno_recv(&t->parse_buffer) < 0) return -1; /* This call to http_parser_execute will result in invocations of the * on_* family of callbacks. The most interesting of these is * on_body_fill_buffer, which is called when data is ready to be copied * into the target buffer. We need to marshal the buffer, buf_size, and * bytes_read parameters to this callback. */ ctx.t = t; ctx.s = s; ctx.buffer = buffer; ctx.buf_size = buf_size; ctx.bytes_read = bytes_read; /* Set the context, call the parser, then unset the context. */ t->parser.data = &ctx; bytes_parsed = http_parser_execute(&t->parser, &t->settings, t->parse_buffer.data, t->parse_buffer.offset); t->parser.data = NULL; /* If there was a handled authentication failure, then parse_error * will have signaled us that we should replay the request. */ if (PARSE_ERROR_REPLAY == t->parse_error) { s->sent_request = 0; if (http_connect(t) < 0) return -1; goto replay; } if (t->parse_error < 0) return -1; if (bytes_parsed != t->parse_buffer.offset) { giterr_set(GITERR_NET, "HTTP parser error: %s", http_errno_description((enum http_errno)t->parser.http_errno)); return -1; } } return 0; }
static void http_basic_test(void) { struct timeval tv; struct bufferevent *bev; int fd; const char *http_request; short port = -1; test_ok = 0; fprintf(stdout, "Testing Basic HTTP Server: "); http = http_setup(&port, NULL); /* bind to a second socket */ if (evhttp_bind_socket(http, "127.0.0.1", port + 1) == -1) { fprintf(stdout, "FAILED (bind)\n"); exit(1); } fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); /* first half of the http request */ http_request = "GET /test HTTP/1.1\r\n" "Host: some"; bufferevent_write(bev, http_request, strlen(http_request)); timerclear(&tv); tv.tv_usec = 10000; event_once(-1, EV_TIMEOUT, http_complete_write, bev, &tv); event_dispatch(); if (test_ok != 3) { fprintf(stdout, "FAILED\n"); exit(1); } /* connect to the second port */ bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); fd = http_connect("127.0.0.1", port + 1); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); http_request = "GET /test HTTP/1.1\r\n" "Host: somehost\r\n" "Connection: close\r\n" "\r\n"; bufferevent_write(bev, http_request, strlen(http_request)); event_dispatch(); bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); evhttp_free(http); if (test_ok != 5) { fprintf(stdout, "FAILED\n"); exit(1); } fprintf(stdout, "OK\n"); }
// main(argc, argv) // The main loop. int main(int argc, char** argv) { // parse arguments int ch, nocheck = 0; while ((ch = getopt(argc, argv, "nh:p:u:")) != -1) { if (ch == 'h') pong_host = optarg; else if (ch == 'p') pong_port = optarg; else if (ch == 'u') pong_user = optarg; else if (ch == 'n') nocheck = 1; else usage(); } if (optind == argc - 1) pong_user = argv[optind]; else if (optind != argc) usage(); // look up network address of pong server struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICSERV; int r = getaddrinfo(pong_host, pong_port, &hints, &pong_addr); if (r != 0) { fprintf(stderr, "problem looking up %s: %s\n", pong_host, gai_strerror(r)); exit(1); } // reset pong board and get its dimensions int width, height; { http_connection* conn = http_connect(pong_addr); http_send_request(conn, nocheck ? "reset?nocheck=1" : "reset"); http_receive_response_headers(conn); http_receive_response_body(conn); if (conn->status_code != 200 || sscanf(conn->buf, "%d %d\n", &width, &height) != 2 || width <= 0 || height <= 0) { fprintf(stderr, "bad response to \"reset\" RPC: %d %s\n", conn->status_code, http_truncate_response(conn)); exit(1); } http_close(conn); } // measure future times relative to this moment elapsed_base = timestamp(); // print display URL printf("Display: http://%s:%s/%s/%s\n", pong_host, pong_port, pong_user, nocheck ? " (NOCHECK mode)" : ""); // initialize global synchronization objects pthread_mutex_init(&mutex, NULL); pthread_cond_init(&condvar, NULL); // play game int x = 0, y = 0, dx = 1, dy = 1; while (1) { // create a new thread to handle the next position pong_args pa; pa.x = x; pa.y = y; pthread_t pt; r = pthread_create(&pt, NULL, pong_thread, &pa); if (r != 0) { fprintf(stderr, "%.3f sec: pthread_create: %s\n", elapsed(), strerror(r)); exit(1); } // wait until that thread signals us to continue pthread_mutex_lock(&mutex); pthread_cond_wait(&condvar, &mutex); pthread_mutex_unlock(&mutex); // update position x += dx; y += dy; if (x < 0 || x >= width) { dx = -dx; x += 2 * dx; } if (y < 0 || y >= height) { dy = -dy; y += 2 * dy; } // wait 0.1sec usleep(100000); } }
static void http_chunked_test(void) { struct bufferevent *bev; int fd; const char *http_request; short port = -1; struct timeval tv_start, tv_end; struct evhttp_connection *evcon = NULL; struct evhttp_request *req = NULL; int i; test_ok = 0; fprintf(stdout, "Testing Chunked HTTP Reply: "); http = http_setup(&port, NULL); fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_chunked_readcb, http_chunked_writecb, http_chunked_errorcb, NULL); http_request = "GET /chunked HTTP/1.1\r\n" "Host: somehost\r\n" "Connection: close\r\n" "\r\n"; bufferevent_write(bev, http_request, strlen(http_request)); evutil_gettimeofday(&tv_start, NULL); event_dispatch(); evutil_gettimeofday(&tv_end, NULL); evutil_timersub(&tv_end, &tv_start, &tv_end); if (tv_end.tv_sec >= 1) { fprintf(stdout, "FAILED (time)\n"); exit (1); } if (test_ok != 2) { fprintf(stdout, "FAILED\n"); exit(1); } /* now try again with the regular connection object */ evcon = evhttp_connection_new("127.0.0.1", port); if (evcon == NULL) { fprintf(stdout, "FAILED\n"); exit(1); } /* make two requests to check the keepalive behavior */ for (i = 0; i < 2; i++) { test_ok = 0; req = evhttp_request_new(http_chunked_request_done, NULL); /* Add the information that we care about */ evhttp_add_header(req->output_headers, "Host", "somehost"); /* We give ownership of the request to the connection */ if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/chunked") == -1) { fprintf(stdout, "FAILED\n"); exit(1); } event_dispatch(); if (test_ok != 1) { fprintf(stdout, "FAILED\n"); exit(1); } } evhttp_connection_free(evcon); evhttp_free(http); fprintf(stdout, "OK\n"); }
int dav_connect(HTTP_CONNECTION **connection, const char *host, short port, const char *username, const char *password) { return http_connect(connection, host, port, username, password) == HT_OK; }
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ascp $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/ int main(int argc, char *argv[]) { int bytes_buffered, fd, hunk_size; off_t bytes_read = 0; char block[1024], hostname[45], path[MAX_PATH_LENGTH]; if ((argc != 2) && (argc != 3) && (argc != 4)) { (void)fprintf(stderr, "Usage: %s <filename> [<host> [<path>]]\n", argv[0]); exit(-1); } if (argc == 4) { (void)strncpy(hostname, argv[2], 44); (void)strncpy(path, argv[3], MAX_PATH_LENGTH - 1); } else if (argc == 3) { (void)strncpy(hostname, argv[2], 44); (void)strcpy(path, "/"); } else { (void)strcpy(hostname, "localhost"); (void)strcpy(path, "/"); } if (http_connect(hostname, DEFAULT_HTTP_PORT) == -1) { (void)fprintf(stderr, "http_connect() failed\n"); exit(-1); } if (http_get(hostname, path, argv[1], &bytes_buffered) == INCORRECT) { (void)fprintf(stderr, "http_get() failed\n"); exit(-1); } if ((fd = open(argv[1], (O_WRONLY | O_CREAT | O_TRUNC), (S_IRUSR | S_IWUSR))) == -1) { (void)fprintf(stderr, "Failed to open() %s : %s\n", argv[1], strerror(errno)); exit(-1); } if (bytes_buffered > 0) { bytes_read += bytes_buffered; if (write(fd, msg_str, bytes_buffered) != bytes_buffered) { (void)fprintf(stderr, "write() error : %s\n", strerror(errno)); exit(-1); } } while ((hunk_size = http_read(block, 1024)) > 0) { if (write(fd, block, hunk_size) != hunk_size) { (void)fprintf(stderr, "write() error : %s\n", strerror(errno)); exit(-1); } bytes_read += hunk_size; } if (close(fd) == -1) { (void)fprintf(stderr, "close() error : %s\n", strerror(errno)); } (void)fprintf(stdout, "Got file %s with %d Bytes.\n", argv[1], bytes_read); return(0); }
int main(int argc, char **argv) { char *host = "125.211.218.8"; char *path = "/techqq/zt/2007/firacn/topic_html/xsm.htm"; /* char *host = "10.205.42.139"; char *path = "/techqq/a/20090423/000378.htm"; char *path = "/techqq/a/20120111/000508.htm"; char *path = "/techqq/a/20121008/000013.htm"; char *path = "/techqq/wlyx.htm"; char *path = "/techqq/index.html"; char *path = "/techqq/a/20121008/000048.htm"; 10.205.42.139:80/techqq/a/20090423/000378.htm */ const char *body_ptr; int status; http_client_t http_client1; http_connect(&http_client1, host, 80); http_do_get(&http_client1, path); status = http_response_status(&http_client1); body_ptr = http_response_body(&http_client1); printf("---------------------Status-----------------------\n"); printf("%d\n", status); printf("---------------------Body-----------------------\n"); printf("%p\n", body_ptr); printf("%s\n", body_ptr); http_disconnect(&http_client1); /* status = http_response_status(&http_client1); body_ptr = http_response_body(&http_client1); */ /* printf("---------------------Status-----------------------\n"); printf("%d\n", status); printf("---------------------Body-----------------------\n"); printf("%s\n", body_ptr); */ /* http_client_t http_client2; http_connect(&http_client2, "ufp.umeng.com", 80); http_do_get(&http_client2, "/login"); status = http_response_status(&http_client2); body_ptr = http_response_body(&http_client2); printf("---------------------Status-----------------------\n"); printf("%d\n", status); printf("---------------------Body-----------------------\n"); printf("%s\n", body_ptr); http_disconnect(&http_client2); http_client_t http_client3; http_connect(&http_client3, "blog.umeng.com", 80); http_do_get(&http_client3, "/index.php/category/products/"); status = http_response_status(&http_client3); body_ptr = http_response_body(&http_client3); printf("---------------------Status-----------------------\n"); printf("%d\n", status); printf("---------------------Body-----------------------\n"); printf("%s\n", body_ptr); http_disconnect(&http_client3); */ }
/* return non zero if error */ static int http_open(URLContext *h, const char *uri, int flags) { const char *path, *proxy_path; char hostname[1024], hoststr[1024]; char auth[1024]; char path1[1024]; char buf[1024]; int port, use_proxy, err; HTTPContext *s; URLContext *hd = NULL; h->is_streamed = 1; s = av_malloc(sizeof(HTTPContext)); if (!s) { return -ENOMEM; } h->priv_data = s; proxy_path = getenv("http_proxy"); use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && strstart(proxy_path, "http://", NULL); /* fill the dest addr */ redo: /* needed in any case to build the host string */ url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), uri); if (port > 0) { snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); } else { pstrcpy(hoststr, sizeof(hoststr), hostname); } if (use_proxy) { url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, NULL, 0, proxy_path); path = uri; } else { if (path1[0] == '\0') path = "/"; else path = path1; } if (port < 0) port = 80; snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); err = url_open(&hd, buf, URL_RDWR); if (err < 0) goto fail; s->hd = hd; if (http_connect(h, path, hoststr, auth) < 0) goto fail; if (s->http_code == 303 && s->location[0] != '\0') { /* url moved, get next */ uri = s->location; url_close(hd); goto redo; } return 0; fail: if (hd) url_close(hd); av_free(s); return AVERROR_IO; }
END_TEST #if HAVE_EVHTTP_H START_TEST(test_http) { struct parent_msg msg = {}; const char *errstr = NULL; extern char *http_host, *http_path; static struct event_base *base; struct evhttp *httpd; extern struct evhttp_request *lreq; extern struct evhttp_connection *evcon; extern int status; extern short http_port; int sock = -1; // check for ipv4 before running the test mark_point(); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) return; else close(sock); // this assumes libevent can connect to localhost // sounds silly, but I've seen it fail... http_host = "127.0.0.1"; http_path = "/cgi-bin/test.cgi"; event_set_log_callback(&fake_log_cb); mark_point(); base = event_init(); httpd = evhttp_new(base); for (http_port = 8080; http_port < 8090; http_port++) { if (evhttp_bind_socket(httpd, http_host, http_port) != -1) break; } fail_unless (http_port < 8090, "failed to start httpd on %s", http_host); // If either of these two fail then we're screwed anyway mark_point(); http_connect(); mark_point(); http_request(&msg, 0); mark_point(); strlcpy(msg.name, "eth0", IFNAMSIZ); msg.proto = PROTO_CDP; msg.decode = (1 << PEER_HOSTNAME)|(1 << PEER_PORTNAME); msg.peer[PEER_HOSTNAME] = strdup("router"); msg.peer[PEER_PORTNAME] = strdup("Fas'tEthernet42/64"); http_request(&msg, 0); mark_point(); errstr = "HTTP request failed"; my_log(CRIT, "check"); WRAP_FATAL_START(); http_reply(lreq, NULL); WRAP_FATAL_END(); fail_unless (strncmp(check_wrap_errstr, errstr, strlen(errstr)) == 0, "incorrect message logged: %s", check_wrap_errstr); mark_point(); lreq->response_code = 200; http_reply(lreq, NULL); fail_unless (status == EXIT_SUCCESS, "incorrect exit status returned: %d", status); mark_point(); lreq->response_code = 404; errstr = "HTTP error 404 received"; my_log(CRIT, "check"); http_reply(lreq, NULL); fail_unless (strncmp(check_wrap_errstr, errstr, strlen(errstr)) == 0, "incorrect message logged: %s", check_wrap_errstr); fail_unless (status == EXIT_FAILURE, "incorrect exit status returned: %d", status); mark_point(); evhttp_connection_free(evcon); lreq = NULL; mark_point(); errstr = "failed"; my_log(CRIT, "check"); evcon = evhttp_connection_new("256.256.256.256", 0); WRAP_FATAL_START(); http_request(&msg, 0); WRAP_FATAL_END(); fail_unless (strstr(check_wrap_errstr, errstr) != NULL, "incorrect message logged: %s", check_wrap_errstr); evhttp_connection_free(evcon); mark_point(); errstr = "failed"; my_log(CRIT, "check"); evcon = evhttp_connection_new("localhost", 0); WRAP_FATAL_START(); http_request(&msg, 0); http_dispatch(); WRAP_FATAL_END(); fail_unless (strstr(check_wrap_errstr, errstr) != NULL, "incorrect message logged: %s", check_wrap_errstr); mark_point(); // free the active connection evhttp_connection_free(evcon); lreq = NULL; evcon = evhttp_connection_new(http_host, 80); http_dispatch(); evhttp_free(httpd); event_base_free(base); peer_free(msg.peer); }
int crawler_crawl(link_crawler_t *crawler, char *url, list_t *link_list) { http_url_t http_url; int status = CRAWLER_NULL; if(http_url_parse_s(&http_url, url) == URL_RECOGNIZED) { int port = strlen(http_url.port) != 0 ? atoi(http_url.port) : 80; char path[4096] = {""}; strlen(http_url.search) == 0 ? sprintf(path, "%s", http_url.path) : sprintf(path, "%s?%s", http_url.path, http_url.search); if(http_connect(&crawler->http_client, http_url.host, port) == CONNECT_OK) { int ecode, response_status; const char *page = NULL; printf("%s\n", url); ecode = http_do_get(&crawler->http_client, path); switch(ecode) { case RESPONSE_OK: response_status = http_response_status(&crawler->http_client); if(response_status == HTTP_OK) { /* get entity_body pointer */ page = http_response_body(&crawler->http_client); /* extrack link from buffer and save link into list*/ if(page != NULL) { extract_absolute_link_s(page, link_list, url); status = CRAWLER_OK; } else { status = CRAWLER_NULL; fprintf(stderr, "Request %s:%d%s failed, response body is null.\n", crawler->http_client.connection.host, crawler->http_client.connection.port, path); } } else { fprintf(stderr, "Request %s:%d%s failed, Status code: %d.\n", crawler->http_client.connection.host, crawler->http_client.connection.port, path, response_status); status = CRAWLER_NONEED; } break; case RESPONSE_OVERFLOW: fprintf(stderr, "Request %s:%d%s do_get receive overflow.\n", crawler->http_client.connection.host, crawler->http_client.connection.port, path); status = CRAWLER_OVERFLOW; break; case RESPONSE_FAILED: fprintf(stderr, "Request %s:%d%s do_get receive break.\n", crawler->http_client.connection.host, crawler->http_client.connection.port, path); status = CRAWLER_BREAK; break; case RESPONSE_TIMEOUT: fprintf(stderr, "Request %s:%d%s do_get receive timeout.\n", crawler->http_client.connection.host, crawler->http_client.connection.port, path); status = CRAWLER_TIMEOUT; break; case REQUEST_FAILED: fprintf(stderr, "Request %s:%d%s do_get request failed.\n", http_url.host, port, path); status = CRAWLER_FAILED; break; default: fprintf(stderr, "Unknown ecode %d.\n", ecode); status = CRAWLER_UNKNOWN; break; } http_disconnect(&crawler->http_client); } else { fprintf(stderr, "Http connect %s:%d failed.\n", http_url.host, port); status = CRAWLER_UNREACH; } } else { fprintf(stderr, "Unrecognize url: %s\n", url); status = CRAWLER_UNKNOWN; } return status; }
/* return zero if error */ static void *http_open(vfs_handler_t * vfs, const char *fn, int flags) { const char *path, *proxy_path; char hostname[128]; char path1[256]; int port, use_proxy, err; HTTPContext *s; file_t hd = -1; int wait = 0; if (flags & O_DIR) return NULL; //h->is_streamed = 1; s = malloc(sizeof(HTTPContext)); if (!s) { return NULL; } use_proxy = 0; /* fill the dest addr */ redo: /* needed in any case to build the host string */ _url_split(NULL, 0, hostname, sizeof(hostname), &port, path1, sizeof(path1), fn); if (port > 0) { snprintf(s->hoststr, sizeof(s->hoststr), "%s:%d", hostname, port); } else { pstrcpy(s->hoststr, sizeof(s->hoststr), hostname); } if (use_proxy) { _url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, proxy_path); path = fn; } else { if (path1[0] == '\0') path = "/"; else path = path1; } if (port < 0) port = 80; snprintf(s->location, sizeof(s->location), "/tcp/%s:%d", hostname, port); #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS : opening '%s' '%s'\n", s->location, path); #endif redo2: hd = fs_open(s->location, O_RDWR); err = hd >= 0 ? 0:-1; if (err < 0) goto fail; s->hd = hd; strcpy(s->path, path); s->flags = flags; if (http_connect(s, path, s->hoststr, flags, wait) < 0) { if (0 && wait <= 2000) { /* try again with a sleep */ wait += 1000; fs_close(hd); hd = -1; goto redo2; } goto fail; } if ((s->http_code == 303 || s->http_code == 302) && s->location[0] != '\0') { /* url moved, get next */ fn = s->location+6; #ifdef DEBUG dbglog(DBG_DEBUG, "URL moved get next '%s'\n", fn); #endif fs_close(hd); hd = -1; goto redo; } if (s->http_code != 200) goto fail; return (void*) s; fail: if (hd >= 0) fs_close(hd); free(s); return NULL; }
/* return zero if error */ static uint32 http_open(vfs_handler_t * dummy, const char *uri, int flags) { const char *path, *proxy_path; char hostname[128]; char path1[256]; int port, use_proxy, err; HTTPContext *s; file_t hd = -1; int wait = 0; if (flags & O_DIR) return 0; //h->is_streamed = 1; s = malloc(sizeof(HTTPContext)); if (!s) { return 0; } /* proxy_path = getenv("http_proxy"); */ /* use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && */ /* strstart(proxy_path, "http://", NULL); */ use_proxy = 0; s->nb_header_entries = 0; /* fill the dest addr */ redo: /* needed in any case to build the host string */ url_split(NULL, 0, hostname, sizeof(hostname), &port, path1, sizeof(path1), uri); if (port > 0) { snprintf(s->hoststr, sizeof(s->hoststr), "%s:%d", hostname, port); } else { pstrcpy(s->hoststr, sizeof(s->hoststr), hostname); } if (use_proxy) { url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, proxy_path); path = uri; } else { if (path1[0] == '\0') path = "/"; else path = path1; } if (port < 0) port = 80; snprintf(s->location, sizeof(s->location), "/tcp/%s:%d", hostname, port); //printf("HTTPFS : opening '%s' '%s'\n", s->location, path); redo2: hd = fs_open(s->location, O_RDWR); err = hd >= 0? 0:-1; // err = url_open(&hd, buf, URL_RDWR); if (err < 0) goto fail; s->hd = hd; strcpy(s->path, path); s->flags = flags; if (http_connect(s, path, s->hoststr, flags, wait) < 0) { if (0&&wait <= 2000) { /* try again with a sleep */ wait += 1000; fs_close(hd); hd = -1; goto redo2; } goto fail; } if ((s->http_code == 303 || s->http_code == 302) && s->location[0] != '\0') { /* url moved, get next */ uri = s->location+6; #ifdef DEBUG printf("URL moved get next '%s'\n", uri); #endif fs_close(hd); hd = -1; //wait = 4000; goto redo; } if (s->http_code != 200) goto fail; return (uint32) s; fail: hdr_clear(s); if (hd>=0) fs_close(hd); free(s); return 0; }
// pong_thread(threadarg) // Connect to the server at the position indicated by `threadarg` // (which is a pointer to a `pong_args` structure). void* pong_thread(void* threadarg) { pthread_detach(pthread_self()); // Copy thread arguments onto our stack. pong_args pa = *((pong_args*) threadarg); char url[256]; snprintf(url, sizeof(url), "move?x=%d&y=%d&style=on", pa.x, pa.y); http_connection* conn; size_t sleeptime = 10000; retry: // find reusable connection using LIFO access pthread_mutex_lock(&table_lock); if(conn_done_num > 0) { conn = conn_done_table[conn_done_num - 1]; --conn_done_num; pthread_mutex_unlock(&table_lock); } else { // if not, create a new connection pthread_mutex_unlock(&table_lock); conn = http_connect(pong_addr); } // let other threads wait for a sleeping thread due to server down pthread_mutex_lock(&time_lock); while(stop_time != 0) { pthread_cond_wait(&stop_time_cond, &time_lock); } pthread_mutex_unlock(&time_lock); // send request after the server has waken up http_send_request(conn, url); http_receive_response_headers(conn); // failed connection while(conn->state == HTTP_BROKEN && conn->status_code == -1) { http_close(conn); // usleep less than 1 second if(sleeptime < 1000000) usleep(sleeptime); else { // sleep longer than a second sleep(1); usleep(sleeptime % 1000000); } // keep sleep time less than 2 seconds and double each retry if(sleeptime <= 1000000) sleeptime += sleeptime; goto retry; } if (conn->status_code != 200) fprintf(stderr, "%.3f sec: warning: %d,%d: " "server returned status %d (expected 200)\n", elapsed(), pa.x, pa.y, conn->status_code); // signal more thread creation after having received header pthread_cond_signal(&condvar); http_receive_response_body(conn); // if server sends STOP, read the time in millisecond to stop_time pthread_mutex_lock(&time_lock); if(stop_time == 0 && sscanf(http_truncate_response(conn), "+%d STOP", &stop_time) && stop_time != 0) { pthread_mutex_unlock(&time_lock); // sleep less than 1 second if(stop_time < 1000) usleep(stop_time * 1000); else { // sleep longer than 1 second sleep(stop_time / 1000); usleep((stop_time % 1000) * 1000); } pthread_mutex_lock(&time_lock); stop_time = 0; // woke up, tell all threads to continue pthread_cond_broadcast(&stop_time_cond); pthread_mutex_unlock(&time_lock); } else if (stop_time != 0) { while(stop_time != 0) { pthread_cond_wait(&stop_time_cond, &time_lock); } if(sscanf(http_truncate_response(conn), "+%d STOP", &stop_time) && stop_time != 0) { pthread_mutex_unlock(&time_lock); if(stop_time < 1000) usleep(stop_time * 1000); else { sleep(stop_time / 1000); usleep((stop_time % 1000) * 1000); } pthread_mutex_lock(&time_lock); stop_time = 0; pthread_cond_broadcast(&stop_time_cond); pthread_mutex_unlock(&time_lock); } else { pthread_mutex_unlock(&time_lock); } } else { pthread_mutex_unlock(&time_lock); } double result = strtod(conn->buf, NULL); if (result < 0) { fprintf(stderr, "%.3f sec: server returned error: %s\n", elapsed(), http_truncate_response(conn)); exit(1); } // if the connection has status done, keep it in the connection table pthread_mutex_lock(&table_lock); if(conn->state == HTTP_DONE && conn_done_num < 29) { conn_done_table[conn_done_num] = conn; ++conn_done_num; pthread_mutex_unlock(&table_lock); } else { pthread_mutex_unlock(&table_lock); http_close(conn); } // signal the main thread to continue // pthread_cond_signal(&condvar); // and exit! pthread_exit(NULL); }
/** * Simple command-line HTTP client. */ int main( int argc, char *argv[ ] ) { int client_connection; char *host, *path; char *proxy_host, *proxy_user, *proxy_password; int proxy_port; struct hostent *host_name; struct sockaddr_in host_address; int port = HTTPS_PORT; int ind; int master_secret_length; unsigned char *master_secret; int session_id_length; unsigned char *session_id; #ifdef WIN32 WSADATA wsaData; #endif TLSParameters tls_context; if ( argc < 2 ) { fprintf( stderr, "Usage: %s: [-p http://[username:password@]proxy-host:proxy-port] <URL>\n", argv[ 0 ] ); return 1; } proxy_host = proxy_user = proxy_password = host = path = session_id = master_secret = NULL; session_id_length = master_secret_length = 0; for ( ind = 1; ind < ( argc - 1 ); ind++ ) { if ( !strcmp( "-p", argv[ ind ] ) ) { if ( !parse_proxy_param( argv[ ++ind ], &proxy_host, &proxy_port, &proxy_user, &proxy_password ) ) { fprintf( stderr, "Error - malformed proxy parameter '%s'.\n", argv[ 2 ] ); return 2; } } else if ( !strcmp( "-s", argv[ ind ] ) ) { session_id_length = hex_decode( argv[ ++ind ], &session_id ); } else if ( !strcmp( "-m", argv[ ind ] ) ) { master_secret_length = hex_decode( argv[ ++ind ], &master_secret ); } } if ( ( ( master_secret_length > 0 ) && ( session_id_length == 0 ) ) || ( ( master_secret_length == 0 ) && ( session_id_length > 0 ) ) ) { fprintf( stderr, "session id and master secret must both be provided.\n" ); return 3; } if ( parse_url( argv[ ind ], &host, &path ) == -1 ) { fprintf( stderr, "Error - malformed URL '%s'.\n", argv[ 1 ] ); return 1; } printf( "Connecting to host '%s'\n", host ); // Step 1: open a socket connection on http port with the destination host. #ifdef WIN32 if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != NO_ERROR ) { fprintf( stderr, "Error, unable to initialize winsock.\n" ); return 2; } #endif client_connection = socket( PF_INET, SOCK_STREAM, 0 ); if ( !client_connection ) { perror( "Unable to create local socket" ); return 2; } if ( proxy_host ) { printf( "Connecting to host '%s'\n", proxy_host ); host_name = gethostbyname( proxy_host ); } else { host_name = gethostbyname( host ); } if ( !host_name ) { perror( "Error in name resolution" ); return 3; } host_address.sin_family = AF_INET; host_address.sin_port = htons( proxy_host ? proxy_port : HTTPS_PORT ); memcpy( &host_address.sin_addr, host_name->h_addr_list[ 0 ], sizeof( struct in_addr ) ); if ( connect( client_connection, ( struct sockaddr * ) &host_address, sizeof( host_address ) ) == -1 ) { perror( "Unable to connect to host" ); return 4; } printf( "Connection complete; negotiating TLS parameters\n" ); if ( proxy_host ) { if ( !http_connect( client_connection, host, port, proxy_user, proxy_password ) ) { perror( "Unable to establish proxy tunnel" ); if ( close( client_connection ) == -1 ) { perror( "Error closing client connection" ); return 2; } return 3; } } if ( session_id != NULL ) { if ( tls_resume( client_connection, session_id_length, session_id, master_secret, &tls_context ) ) { fprintf( stderr, "Error: unable to negotiate SSL connection.\n" ); if ( close( client_connection ) == -1 ) { perror( "Error closing client connection" ); return 2; } return 3; } } else { if ( tls_connect( client_connection, &tls_context, 0 ) ) { fprintf( stderr, "Error: unable to negotiate TLS connection.\n" ); return 3; } } printf( "Retrieving document: '%s'\n", path ); http_get( client_connection, path, host, &tls_context ); display_result( client_connection, &tls_context ); tls_shutdown( client_connection, &tls_context ); printf( "Session ID was: " ); show_hex( tls_context.session_id, tls_context.session_id_length ); printf( "Master secret was: " ); show_hex( tls_context.master_secret, MASTER_SECRET_LENGTH ); printf( "Shutting down.\n" ); #ifdef WIN32 if ( closesocket( client_connection ) == -1 ) #else if ( close( client_connection ) == -1 ) #endif { perror( "Error closing client connection" ); return 5; } if ( session_id != NULL ) { free( session_id ); } if ( master_secret != NULL ) { free( master_secret ); } #ifdef WIN32 WSACleanup(); #endif return 0; }
static off_t http_seek(void *hnd, off_t offset, int whence) { HTTPContext *s = (HTTPContext *)hnd; file_t hd; #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: seek %d %d\n", offset, whence); #endif if (whence == SEEK_SET) { whence = SEEK_CUR; offset = offset - s->tread; } if (whence == SEEK_CUR) { int pos = fs_seek(s->hd, 0, SEEK_CUR); int res = fs_seek(s->hd, offset, SEEK_CUR) - pos; #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: seek res = %d\n", res); #endif s->tread += res; return s->tread; } if (whence != SEEK_SET) return -1; fs_close(s->hd); s->hd = -1; #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: location = '%s'\n", s->location); #endif hd = fs_open(s->location, O_RDWR); if (hd < 0) { #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: seek: could not reconnect to '%s'\n", s->location); #endif goto fail; } s->hd = hd; if (http_connect(s, s->path, s->hoststr, s->flags, 0) < 0) goto fail; if (offset) { int pos = fs_seek(s->hd, 0, SEEK_CUR); s->tread = fs_seek(s->hd, offset, SEEK_CUR) - pos; #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: seek res = %d\n", s->tread); #endif return s->tread; } s->tread = 0; return 0; fail: #ifdef DEBUG dbglog(DBG_DEBUG, "HTTPFS: seek: failed reopen\n"); #endif if (s->hd >= 0) fs_close(s->hd); s->hd = 0; return -1; }
/* return non zero if error */ static int http_open_cnx(URLContext *h) { const char *path, *proxy_path; char hostname[1024], hoststr[1024]; char auth[1024]; char path1[1024]; char buf[1024]; int port, use_proxy, err, location_changed = 0, redirects = 0; HTTPContext *s = h->priv_data; URLContext *hd = NULL; proxy_path = getenv("http_proxy"); use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && av_strstart(proxy_path, "http://", NULL); /* fill the dest addr */ redo: /* needed in any case to build the host string */ url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); if (port > 0) { snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); } else { av_strlcpy(hoststr, hostname, sizeof(hoststr)); } if (use_proxy) { url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, NULL, 0, proxy_path); path = s->location; } else { if (path1[0] == '\0') path = "/"; else path = path1; } if (port < 0) port = 80; snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); err = url_open(&hd, buf, URL_RDWR); if (err < 0) goto fail; s->hd = hd; if (http_connect(h, path, hoststr, auth, &location_changed) < 0) goto fail; if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) { /* url moved, get next */ url_close(hd); if (redirects++ >= MAX_REDIRECTS) return AVERROR(EIO); location_changed = 0; goto redo; } return 0; fail: if (hd) url_close(hd); return AVERROR(EIO); }
/* return non zero if error */ static int http_open_cnx(URLContext *h) { const char *path, *proxy_path; char hostname[1024], hoststr[1024]; char auth[1024]; char path1[1024]; char buf[1024]; int port, use_proxy, err, location_changed = 0, redirects = 0; HTTPAuthType cur_auth_type; HTTPContext *s = h->priv_data; URLContext *hd = NULL; proxy_path = getenv("http_proxy"); use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && av_strstart(proxy_path, "http://", NULL); /* fill the dest addr */ redo: /* needed in any case to build the host string */ ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); if (use_proxy) { ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, NULL, 0, proxy_path); path = s->location; } else { if (path1[0] == '\0') path = "/"; else path = path1; } if (port < 0) port = 80; ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); err = url_open(&hd, buf, URL_RDWR); if (err < 0) goto fail; s->hd = hd; cur_auth_type = s->auth_state.auth_type; if (http_connect(h, path, hoststr, auth, &location_changed) < 0) goto fail; if (s->http_code == 401) { if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) { url_close(hd); goto redo; } else { err = AVERROR(EACCES); goto fail; } } if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) { /* url moved, get next */ url_close(hd); if (redirects++ >= MAX_REDIRECTS) return AVERROR(EFAULT); location_changed = 0; goto redo; } return 0; fail: if (hd) url_close(hd); return err; }