From a184f5d1d361f699bab9cc9483c815302dd61be7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 16 Mar 2002 01:34:52 +0000 Subject: * formatting, plus remove some old fopen wrappers --- ext/standard/http_fopen_wrapper.c | 282 ++------------------------------------ 1 file changed, 11 insertions(+), 271 deletions(-) (limited to 'ext/standard/http_fopen_wrapper.c') diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 41ee9d7d54..9cada1fb52 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -70,21 +70,20 @@ #define HTTP_HEADER_BLOCK_SIZE 1024 -#if HAVE_PHP_STREAM -php_stream * php_stream_url_wrap_http(char * path, char * mode, int options, char ** opened_path TSRMLS_DC) +php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char **opened_path TSRMLS_DC) { - php_stream * stream = NULL; - php_url * resource = NULL; + php_stream *stream = NULL; + php_url *resource = NULL; int use_ssl; - char * scratch = NULL; - char * tmp = NULL; + char *scratch = NULL; + char *tmp = NULL; int scratch_len = 0; int body = 0; char location[HTTP_HEADER_BLOCK_SIZE]; - zval * response_header = NULL; + zval *response_header = NULL; int reqok = 0; - char * http_header_line = NULL; + char *http_header_line = NULL; char tmp_line[128]; resource = php_url_parse(path); @@ -180,7 +179,7 @@ php_stream * php_stream_url_wrap_http(char * path, char * mode, int options, cha /* get response header */ if (php_stream_gets(stream, tmp_line, sizeof(tmp_line)-1) != NULL) { - zval * http_response; + zval *http_response; MAKE_STD_ZVAL(http_response); if (strncmp(tmp_line + 8, " 200 ", 5) == 0) @@ -207,7 +206,7 @@ php_stream * php_stream_url_wrap_http(char * path, char * mode, int options, cha while (!body && !php_stream_eof(stream)) { if (php_stream_gets(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) { - char * p; + char *p; int found_eol = 0; int http_header_line_length; @@ -231,7 +230,7 @@ php_stream * php_stream_url_wrap_http(char * path, char * mode, int options, cha if (http_header_line[0] == '\0') body = 1; else { - zval * http_header; + zval *http_header; MAKE_STD_ZVAL(http_header); @@ -299,7 +298,7 @@ out: stream->wrapperdata = response_header; if (response_header) { - zval * sym; + zval *sym; MAKE_STD_ZVAL(sym); *sym = *response_header; zval_copy_ctor(sym); @@ -315,265 +314,6 @@ php_stream_wrapper php_stream_http_wrapper = { NULL }; -#else - - - -/* {{{ php_fopen_url_wrap_http - */ -FILE *php_fopen_url_wrap_http(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path TSRMLS_DC) -{ - FILE *fp=NULL; - php_url *resource=NULL; - char tmp_line[128]; - char location[512]; - char hdr_line[8192]; - int body = 0; - char *scratch; - unsigned char *tmp; - int len; - int reqok = 0; - zval *response_header; - char *http_header_line; - int http_header_line_length, http_header_line_size; - - resource = php_url_parse((char *) path); - if (resource == NULL) { - php_error(E_WARNING, "Invalid URL specified, %s", path); - *issock = BAD_URL; - return NULL; - } - /* use port 80 if one wasn't specified */ - if (resource->port == 0) { - resource->port = 80; - } - - *socketd = php_hostconnect(resource->host, resource->port, SOCK_STREAM, 0); - if (*socketd == -1) { - SOCK_FCLOSE(*socketd); - *socketd = 0; - php_url_free(resource); - return NULL; - } -#if 0 - if ((fp = fdopen(*socketd, "r+")) == NULL) { - php_url_free(resource); - return NULL; - } -#ifdef HAVE_SETVBUF - if ((setvbuf(fp, NULL, _IONBF, 0)) != 0) { - php_url_free(resource); - return NULL; - } -#endif -#endif /*win32 */ - - strcpy(hdr_line, "GET "); - - /* tell remote http which file to get */ - if (resource->path != NULL && *resource->path) { - strlcat(hdr_line, resource->path, sizeof(hdr_line)); - } else { - strlcat(hdr_line, "/", sizeof(hdr_line)); - } - /* append the query string, if any */ - if (resource->query != NULL) { - strlcat(hdr_line, "?", sizeof(hdr_line)); - strlcat(hdr_line, resource->query, sizeof(hdr_line)); - } - strlcat(hdr_line, " HTTP/1.0\r\n", sizeof(hdr_line)); - SOCK_WRITE(hdr_line, *socketd); - - /* send authorization header if we have user/pass */ - if (resource->user != NULL && resource->pass != NULL) { - scratch = (char *) emalloc(strlen(resource->user) + strlen(resource->pass) + 2); - if (!scratch) { - php_url_free(resource); - return NULL; - } - strcpy(scratch, resource->user); - strcat(scratch, ":"); - strcat(scratch, resource->pass); - tmp = php_base64_encode((unsigned char *)scratch, strlen(scratch), NULL); - - if (snprintf(hdr_line, sizeof(hdr_line), - "Authorization: Basic %s\r\n", tmp) > 0) { - SOCK_WRITE(hdr_line, *socketd); - } - - efree(scratch); - efree(tmp); - } - /* if the user has configured who they are, send a From: line */ - if (cfg_get_string("from", &scratch) == SUCCESS) { - if (snprintf(hdr_line, sizeof(hdr_line), - "From: %s\r\n", scratch) > 0) { - SOCK_WRITE(hdr_line, *socketd); - } - - } - /* send a Host: header so name-based virtual hosts work */ - if (resource->port != 80) { - len = snprintf(hdr_line, sizeof(hdr_line), - "Host: %s:%i\r\n", resource->host, resource->port); - } else { - len = snprintf(hdr_line, sizeof(hdr_line), - "Host: %s\r\n", resource->host); - } - if(len > sizeof(hdr_line) - 1) { - len = sizeof(hdr_line) - 1; - } - if (len > 0) { - SOCK_WRITE(hdr_line, *socketd); - } - - /* identify ourselves and end the headers */ - SOCK_WRITE("User-Agent: PHP/" PHP_VERSION "\r\n\r\n", *socketd); - - body = 0; - location[0] = '\0'; - - MAKE_STD_ZVAL(response_header); - array_init(response_header); - - if (!SOCK_FEOF(*socketd)) { - /* get response header */ - if (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, *socketd) != NULL) { - zval *http_response; - - MAKE_STD_ZVAL(http_response); - if (strncmp(tmp_line + 8, " 200 ", 5) == 0) { - reqok = 1; - } - Z_STRLEN_P(http_response) = strlen(tmp_line); - Z_STRVAL_P(http_response) = estrndup(tmp_line, Z_STRLEN_P(http_response)); - if (Z_STRVAL_P(http_response)[Z_STRLEN_P(http_response)-1]=='\n') { - Z_STRVAL_P(http_response)[Z_STRLEN_P(http_response)-1]=0; - Z_STRLEN_P(http_response)--; - if (Z_STRVAL_P(http_response)[Z_STRLEN_P(http_response)-1]=='\r') { - Z_STRVAL_P(http_response)[Z_STRLEN_P(http_response)-1]=0; - Z_STRLEN_P(http_response)--; - } - } - Z_TYPE_P(http_response) = IS_STRING; - zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response, sizeof(zval *), NULL); - } - } - - - /* Read past HTTP headers */ - while (!body && !SOCK_FEOF(*socketd)) { - http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE); - http_header_line_size = HTTP_HEADER_BLOCK_SIZE; - http_header_line_length = 0; - if (SOCK_FGETS(http_header_line, HTTP_HEADER_BLOCK_SIZE-1, *socketd) != NULL) { - char *p; - zend_bool found_eol=0; - zval *http_header; - - http_header_line[HTTP_HEADER_BLOCK_SIZE-1] = '\0'; - - do { - p = http_header_line+http_header_line_length; - while (*p) { - while (*p == '\n' || *p == '\r') { - *p = '\0'; - p--; - found_eol=1; - } - if (found_eol) { - break; - } - p++; - } - if (!found_eol) { - http_header_line_size += HTTP_HEADER_BLOCK_SIZE; - http_header_line_length += HTTP_HEADER_BLOCK_SIZE-1; - http_header_line = erealloc(http_header_line, http_header_line_size); - if (SOCK_FGETS(http_header_line+http_header_line_length, HTTP_HEADER_BLOCK_SIZE-1, *socketd)==NULL) { - http_header_line[http_header_line_length] = 0; - break; - } - } else { - http_header_line_length = p-http_header_line+1; - } - } while (!found_eol); - - if (!strncasecmp(http_header_line, "Location: ", 10)) { - strlcpy(location, http_header_line + 10, sizeof(location)); - } - - if (http_header_line[0] == '\0') { - body = 1; - } - - if (http_header_line_length>0) { - MAKE_STD_ZVAL(http_header); - Z_STRVAL_P(http_header) = http_header_line; - Z_STRLEN_P(http_header) = http_header_line_length; - Z_TYPE_P(http_header) = IS_STRING; - zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL); - } else { - efree(http_header_line); - } - } - } - - if (!reqok) { - SOCK_FCLOSE(*socketd); - *socketd = 0; - if (location[0] != '\0') { - zval **response_header_new, *entry, **entryp; - char new_path[512]; - - *new_path='\0'; - if (strlen(location)<8 || strncasecmp(location, "http://", 7)) { - strcpy(new_path, "http://"); - strlcat(new_path, resource->host, sizeof(new_path)); - if (resource->port != 80) { - snprintf(new_path+strlen(new_path), sizeof(new_path)-strlen(new_path)-1, ":%d", resource->port); - } - if (*location != '/') { - php_dirname(resource->path, strlen(resource->path)); - snprintf (new_path+strlen(new_path), sizeof(new_path)-strlen(new_path)-1, "%s/", resource->path); - } - strlcat(new_path, location, sizeof(new_path)); - } - else { - strlcpy(new_path, location, sizeof(new_path)); - } - php_url_free(resource); - fp = php_fopen_url_wrap_http(new_path, mode, options, issock, socketd, opened_path TSRMLS_CC); - if (zend_hash_find(EG(active_symbol_table), "http_response_header", sizeof("http_response_header"), (void **) &response_header_new) == SUCCESS) { - entryp = &entry; - MAKE_STD_ZVAL(entry); - ZVAL_EMPTY_STRING(entry); - zend_hash_next_index_insert(Z_ARRVAL_P(response_header), entryp, sizeof(zval *), NULL); - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(response_header_new)); - while (zend_hash_get_current_data(Z_ARRVAL_PP(response_header_new), (void **)&entryp) == SUCCESS) { - zval_add_ref(entryp); - zend_hash_next_index_insert(Z_ARRVAL_P(response_header), entryp, sizeof(zval *), NULL); - zend_hash_move_forward(Z_ARRVAL_PP(response_header_new)); - } - } - goto out; - } else { - php_url_free(resource); - fp = NULL; - goto out; - } - } - php_url_free(resource); - *issock = 1; - out: - { - ZEND_SET_SYMBOL(EG(active_symbol_table), "http_response_header", response_header); - } - return (fp); -} -/* }}} */ - -#endif /* * Local variables: -- cgit v1.2.1