diff options
author | Werner Koch <[email protected]> | 1999-01-19 18:37:41 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 1999-01-19 18:37:41 +0000 |
commit | ce650acf1fc32a20db50a996ecc3008f47b02188 (patch) | |
tree | eeafe3ce39fcbdc17a377470914942920c6d225b /util | |
parent | See ChangeLog: Sun Jan 17 11:04:33 CET 1999 Werner Koch (diff) | |
download | gnupg-ce650acf1fc32a20db50a996ecc3008f47b02188.tar.gz gnupg-ce650acf1fc32a20db50a996ecc3008f47b02188.zip |
See ChangeLog: Tue Jan 19 19:34:58 CET 1999 Werner Koch
Diffstat (limited to 'util')
-rw-r--r-- | util/ChangeLog | 15 | ||||
-rw-r--r-- | util/http.c | 126 | ||||
-rw-r--r-- | util/iobuf.c | 45 |
3 files changed, 123 insertions, 63 deletions
diff --git a/util/ChangeLog b/util/ChangeLog index 1f3a87a24..356e6df86 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,18 @@ +Tue Jan 19 19:34:58 CET 1999 Werner Koch <[email protected]> + + * * iobuf.c (iobuf_push_filter): Allow filters for temp streams + + (iobuf_write_temp): Ditto. + (iobuf_flush_temp): New. + (iobuf_unget_and_close_temp): Removed. + + * http.c (close_http_document): Renamed to http_close(). + (open_http_document): Renamed to http_open_document(). + (http_open): New. + (http_start_data): New. + (http_wait_response): New. + + Sun Jan 17 11:04:33 CET 1999 Werner Koch <[email protected]> * strgutil.c (trim_trailing_ws): New. diff --git a/util/http.c b/util/http.c index 949cdb322..22c5699e7 100644 --- a/util/http.c +++ b/util/http.c @@ -64,57 +64,105 @@ static int connect_server( const char *server, ushort port ); static int write_server( int socket, const char *data, size_t length ); - int -open_http_document( HTTP_HD hd, const char *document, unsigned int flags ) +http_open( HTTP_HD hd, HTTP_REQ_TYPE reqtype, const char *url, + unsigned int flags ) { int rc; - if( flags ) + if( flags || !(reqtype == HTTP_REQ_GET || reqtype == HTTP_REQ_POST) ) return G10ERR_INV_ARG; /* initialize the handle */ memset( hd, 0, sizeof *hd ); hd->socket = -1; + hd->initialized = 1; + hd->req_type = reqtype; + + rc = parse_uri( &hd->uri, url ); + if( !rc ) { + rc = send_request( hd ); + if( !rc ) { + hd->fp_write = iobuf_fdopen( hd->socket , "w" ); + if( hd->fp_write ) + return 0; + rc = G10ERR_GENERAL; + } + } - rc = parse_uri( &hd->uri, document ); - if( rc ) - goto failure; + if( !hd->fp_read && !hd->fp_write ) + close( hd->socket ); + iobuf_close( hd->fp_read ); + iobuf_close( hd->fp_write); + release_parsed_uri( hd->uri ); + hd->initialized = 0; - rc = send_request( hd ); - if( rc ) - goto failure; + return rc; +} + + +void +ttp_start_data( HTTP_HD hd ) +{ + if( !hd->in_data ) { + iobuf_put( hd->fp_write, '\n' ); + hd->in_data = 1; + } +} + + +int +http_wait_response( HTTP_HD hd, unsigned int *ret_status ) +{ + int rc; + + http_start_data( hd ); /* make sure that we are in the data */ + iobuf_flush( hd->fp_write ); + shutdown( hd->socket, 1 ); + hd->in_data = 0; + + hd->socket = dup( hd->socket ); + if( hd->socket == -1 ) + return G10ERR_GENERAL; + iobuf_close( hd->fp_write ); + hd->fp_write = NULL; hd->fp_read = iobuf_fdopen( hd->socket , "r" ); if( !hd->fp_read ) - goto failure; + return G10ERR_GENERAL; rc = parse_response( hd ); - if( rc == -1 ) { /* no response from server */ - /* Hmmm, should we set some errro variable or map errors */ - goto failure; - } + if( !rc && ret_status ) + *ret_status = hd->status_code; - if( !rc ) - hd->is_http_0_9 = 1; - else - hd->status_code = rc ; + return rc; +} - hd->initialized = 1; - return 0; - failure: - if( !hd->fp_read && !hd->fp_write ) - close( hd->socket ); - iobuf_close( hd->fp_read ); - iobuf_close( hd->fp_write); - release_parsed_uri( hd->uri ); +int +http_open_document( HTTP_HD hd, const char *document, unsigned int flags ) +{ + int rc; + + if( flags ) + return G10ERR_INV_ARG; + + rc = http_open( hd, HTTP_REQ_GET, document, 0 ); + if( rc ) + return rc; + + rc = http_wait_response( hd, NULL ); + if( rc ) + http_close( hd ); return rc; } + + + void -close_http_document( HTTP_HD hd ) +http_close( HTTP_HD hd ) { if( !hd || !hd->initialized ) return; @@ -385,12 +433,15 @@ send_request( HTTP_HD hd ) p = build_rel_path( hd->uri ); request = m_alloc( strlen(p) + 20 ); - sprintf( request, "GET %s%s HTTP/1.0\r\n\r\n", *p == '/'? "":"/", p ); + sprintf( request, "%s %s%s HTTP/1.0\r\n\r\n", + hd->req_type == HTTP_REQ_GET ? "GET" : + hd->req_type == HTTP_REQ_HEAD? "HEAD": + hd->req_type == HTTP_REQ_POST? "POST": "OOPS", + *p == '/'? "":"/", p ); m_free(p); rc = write_server( hd->socket, request, strlen(request) ); m_free( request ); - shutdown( hd->socket, 1 ); return rc; } @@ -442,15 +493,12 @@ build_rel_path( PARSED_URI uri ) /*********************** * Parse the response from a server. - * Returns: -1 for no response or error - * 0 for a http 0.9 response - * nnn http 1.0 status code + * Returns: errorcode and sets some fileds in the handle */ static int parse_response( HTTP_HD hd ) { byte *line, *p, *p2; - int status; unsigned maxlen, len; /* Wait for the status line */ @@ -480,9 +528,13 @@ parse_response( HTTP_HD hd ) /* fixme: add HTTP version number check here */ if( (p2 = strpbrk( p, " \t" ) ) ) *p2++ = 0; - if( !isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2]) || p[3] ) - return 0; /* malformed HTTP statuscode - assume HTTP 0.9 */ - status = atoi( p ); + if( !isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2]) || p[3] ) { + /* malformed HTTP statuscode - assume HTTP 0.9 */ + hd->is_http_0_9 = 1; + hd->status_code = 200; + return 0; + } + hd->status_code = atoi( p ); /* skip all the header lines and wait for the empty line */ do { @@ -498,7 +550,7 @@ parse_response( HTTP_HD hd ) *line = 0; } while( len && *line ); - return status; + return 0; } diff --git a/util/iobuf.c b/util/iobuf.c index b62da6dfa..9fba1fc0b 100644 --- a/util/iobuf.c +++ b/util/iobuf.c @@ -38,6 +38,7 @@ typedef struct { char fname[1]; /* name of the file */ } file_filter_ctx_t ; + /* The first partial length header block must be of size 512 * to make it easier (and efficienter) we use a min. block size of 512 * for all chznks (but the last one) */ @@ -95,7 +96,7 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) assert( size ); /* need a buffer */ for(; size; size-- ) { if( (c=getc(fp)) == EOF ) { - if( ferror(fp) ) { + if( ferror(fp) && errno != EPIPE ) { log_error("%s: read error: %s\n", a->fname, strerror(errno)); rc = G10ERR_READ_FILE; @@ -561,12 +562,9 @@ iobuf_fdopen( int fd, const char *mode ) file_filter_ctx_t *fcx; size_t len; - if( strchr( mode, 'w' ) ) - log_bug("iobuf_fdopen: mode `%s' is not supported", mode); - if( !(fp = fdopen(fd, mode)) ) return NULL; - a = iobuf_alloc(1, 8192 ); + a = iobuf_alloc( strchr( mode, 'w')? 2:1, 8192 ); fcx = m_alloc( sizeof *fcx + 20 ); fcx->fp = fp; fcx->print_only_name = 1; @@ -703,7 +701,6 @@ iobuf_fopen( const char *fname, const char *mode ) - /**************** * Register an i/o filter. */ @@ -732,6 +729,9 @@ iobuf_push_filter( IOBUF a, a->filter = NULL; a->filter_ov = NULL; a->filter_eof = 0; + if( a->usage == 3 ) + a->usage = 2; /* make a write stream from a temp stream */ + if( a->usage == 2 ) { /* allocate a fresh buffer for the original stream */ b->d.buf = m_alloc( a->d.size ); b->d.len = 0; @@ -961,7 +961,7 @@ iobuf_flush(IOBUF a) return 0; /*log_debug("iobuf-%d.%d: flush\n", a->no, a->subno );*/ - if( a->usage == 3 ) { /* must increase the size of the temp buffer */ + if( a->usage == 3 ) { /* increase the temp buffer */ char *newbuf; size_t newsize = a->d.size + 8192; @@ -1141,6 +1141,8 @@ iobuf_writestr(IOBUF a, const char *buf ) int iobuf_write_temp( IOBUF a, IOBUF temp ) { + while( temp->chain ) + pop_filter( temp, temp->filter, NULL ); return iobuf_write(a, temp->d.buf, temp->d.len ); } @@ -1158,28 +1160,18 @@ iobuf_temp_to_buffer( IOBUF a, byte *buffer, size_t buflen ) return n; } + /**************** - * unget the contents of the temp io stream to A and close temp - * Could be optimized!! + * Call this function to terminate processing of the temp stream + * without closing it. This removes all filters from the stream + * makes sure that iobuf_get_temp_{buffer,length}() returns correct + * values. */ void -iobuf_unget_and_close_temp( IOBUF a, IOBUF temp ) +iobuf_flush_temp( IOBUF temp ) { - if( a->unget.buf ) { - if( a->unget.start < a->unget.len ) - log_fatal("cannot do any more ungets on this buffer\n"); - /* not yet cleaned up; do it now */ - m_free(a->unget.buf); - a->unget.buf = NULL; - a->nofast &= ~2; - } - a->unget.size = temp->d.len; - a->unget.buf = m_alloc( a->unget.size ); - a->nofast |= 2; - a->unget.len = temp->d.len; - a->unget.start = 0; - memcpy( a->unget.buf, temp->d.buf, a->unget.len ); - iobuf_close(temp); + while( temp->chain ) + pop_filter( temp, temp->filter, NULL ); } @@ -1285,7 +1277,7 @@ iobuf_seek( IOBUF a, ulong newpos ) if( a->chain ) log_debug("pop_filter called in iobuf_seek - please report\n"); while( a->chain ) - pop_filter( a, a->filter, NULL ); + pop_filter( a, a->filter, NULL ); return 0; } @@ -1310,6 +1302,7 @@ get_real_fname( IOBUF a ) return NULL; } + /**************** * Retrieve the filename */ |