1
1
/* http.c - HTTP protocol handler
2
* Copyright (C) 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
2
* Copyright (C) 1999, 2001, 2002, 2003, 2004,
3
* 2005 Free Software Foundation, Inc.
4
5
* This file is part of GnuPG.
16
17
* You should have received a copy of the GNU General Public License
17
18
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21
23
#include <config.h>
68
70
static int insert_escapes( byte *buffer, const byte *string,
69
71
const byte *special );
70
72
static URI_TUPLE parse_tuple( byte *string );
71
static int send_request( HTTP_HD hd, const char *proxy );
73
static int send_request( HTTP_HD hd, const char *auth, const char *proxy );
72
74
static byte *build_rel_path( PARSED_URI uri );
73
75
static int parse_response( HTTP_HD hd );
116
118
* create a radix64 encoded string.
119
/* TODO: This is a duplicate of code in g10/armor.c. Better to use a
120
single copy in strgutil.c */
121
/* TODO: This is a duplicate of code in g10/armor.c modified to do the
122
"=" padding. Better to use a single copy in strgutil.c ? */
122
124
make_radix64_string( const byte *data, size_t len )
134
136
*p++ = bintoasc[(data[0] >> 2) & 077];
135
137
*p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077];
136
138
*p++ = bintoasc[((data[1]<<2)&074)];
138
141
else if( len == 1 ) {
139
142
*p++ = bintoasc[(data[0] >> 2) & 077];
140
143
*p++ = bintoasc[(data[0] <<4)&060];
147
152
http_open( HTTP_HD hd, HTTP_REQ_TYPE reqtype, const char *url,
148
unsigned int flags, const char *proxy )
153
char *auth, unsigned int flags, const char *proxy )
162
167
rc = parse_uri( &hd->uri, url );
164
rc = send_request( hd, proxy );
169
rc = send_request( hd, auth, proxy );
166
171
hd->fp_write = iobuf_sockopen( hd->sock , "w" );
167
172
if( hd->fp_write )
227
http_open_document( HTTP_HD hd, const char *document,
232
http_open_document( HTTP_HD hd, const char *document, char *auth,
228
233
unsigned int flags, const char *proxy )
232
rc = http_open( hd, HTTP_REQ_GET, document, flags, proxy );
237
rc = http_open(hd, HTTP_REQ_GET, document, auth, flags, proxy );
502
507
* Returns 0 if the request was successful
505
send_request( HTTP_HD hd, const char *proxy )
510
send_request( HTTP_HD hd, const char *auth, const char *proxy )
507
512
const byte *server;
508
513
byte *request, *p;
516
char *proxy_authstr=NULL,*authstr=NULL;
513
518
server = *hd->uri->host? hd->uri->host : "localhost";
514
519
port = hd->uri->port? hd->uri->port : 80;
528
533
uri->port? uri->port : 80, 0, NULL );
531
char *x=make_radix64_string(uri->auth,strlen(uri->auth));
532
auth=m_alloc(50+strlen(x));
533
sprintf(auth,"Proxy-Authorization: Basic %s\r\n",x);
537
remove_escapes(uri->auth);
538
x=make_radix64_string(uri->auth,strlen(uri->auth));
539
proxy_authstr=m_alloc(52+strlen(x));
540
sprintf(proxy_authstr,"Proxy-Authorization: Basic %s\r\n",x);
537
544
release_parsed_uri( uri );
547
hd->sock = connect_server( server, port, hd->flags, hd->uri->scheme );
549
if(auth || hd->uri->auth)
541
hd->sock = connect_server( server, port, hd->flags, hd->uri->scheme );
551
char *x,*tempauth=NULL;
544
char *x=make_radix64_string(hd->uri->auth,strlen(hd->uri->auth));
545
auth=m_alloc(50+strlen(x));
546
sprintf(auth,"Authorization: Basic %s\r\n",x);
555
tempauth=m_strdup(auth);
556
remove_escapes(tempauth);
558
else if(hd->uri->auth)
559
remove_escapes(hd->uri->auth);
561
x=make_radix64_string(tempauth?tempauth:hd->uri->auth,
562
strlen(tempauth?tempauth:hd->uri->auth));
563
authstr=m_alloc(52+strlen(x));
564
sprintf(authstr,"Authorization: Basic %s\r\n",x);
551
569
if( hd->sock == -1 )
554
572
p = build_rel_path( hd->uri );
556
request=m_alloc(strlen(server)*2 + strlen(p) + (auth?strlen(auth):0) + 65);
574
request=m_alloc(strlen(server)*2 + strlen(p)
575
+ (authstr?strlen(authstr):0)
576
+ (proxy_authstr?strlen(proxy_authstr):0) + 65);
558
sprintf( request, "%s http://%s:%hu%s%s HTTP/1.0\r\n%s",
578
sprintf( request, "%s http://%s:%hu%s%s HTTP/1.0\r\n%s%s",
559
579
hd->req_type == HTTP_REQ_GET ? "GET" :
560
580
hd->req_type == HTTP_REQ_HEAD? "HEAD":
561
581
hd->req_type == HTTP_REQ_POST? "POST": "OOPS",
562
server, port, *p == '/'? "":"/", p, auth?auth:"" );
582
server, port, *p == '/'? "":"/", p,
583
authstr?authstr:"",proxy_authstr?proxy_authstr:"" );
565
586
char portstr[15];
572
593
hd->req_type == HTTP_REQ_HEAD? "HEAD":
573
594
hd->req_type == HTTP_REQ_POST? "POST": "OOPS",
574
595
*p == '/'? "":"/", p, server, (port!=80)?portstr:"",
580
601
rc = write_server( hd->sock, request, strlen(request) );
581
602
m_free( request );
603
m_free(proxy_authstr);