~ubuntu-branches/ubuntu/hardy/gnupg/hardy-updates

« back to all changes in this revision

Viewing changes to util/http.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-16 16:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051216165739-v0m2d1you6hd8jho
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
15
16
 *
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,
 
20
 * USA.
19
21
 */
20
22
 
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 );
74
76
 
116
118
 * create a radix64 encoded string.
117
119
 */
118
120
 
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 ? */
121
123
static char *
122
124
make_radix64_string( const byte *data, size_t len )
123
125
{
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)];
 
139
        *p++ = '=';
137
140
    }
138
141
    else if( len == 1 ) {
139
142
        *p++ = bintoasc[(data[0] >> 2) & 077];
140
143
        *p++ = bintoasc[(data[0] <<4)&060];
 
144
        *p++ = '=';
 
145
        *p++ = '=';
141
146
    }
142
147
    *p = 0;
143
148
    return buffer;
145
150
 
146
151
int
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 )
149
154
{
150
155
    int rc;
151
156
 
161
166
 
162
167
    rc = parse_uri( &hd->uri, url );
163
168
    if( !rc ) {
164
 
        rc = send_request( hd, proxy );
 
169
        rc = send_request( hd, auth, proxy );
165
170
        if( !rc ) {
166
171
            hd->fp_write = iobuf_sockopen( hd->sock , "w" );
167
172
            if( hd->fp_write )
224
229
 
225
230
 
226
231
int
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 )
229
234
{
230
235
    int rc;
231
236
 
232
 
    rc = http_open( hd, HTTP_REQ_GET, document, flags, proxy );
 
237
    rc = http_open(hd, HTTP_REQ_GET, document, auth, flags, proxy );
233
238
    if( rc )
234
239
        return rc;
235
240
 
502
507
 * Returns 0 if the request was successful
503
508
 */
504
509
static int
505
 
send_request( HTTP_HD hd, const char *proxy )
 
510
send_request( HTTP_HD hd, const char *auth, const char *proxy )
506
511
{
507
512
    const byte *server;
508
513
    byte *request, *p;
509
514
    ushort port;
510
515
    int rc;
511
 
    char *auth=NULL;
 
516
    char *proxy_authstr=NULL,*authstr=NULL;
512
517
 
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 );
529
534
        if(uri->auth)
530
535
          {
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);
 
536
            char *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);
534
541
            m_free(x);
535
542
          }
536
543
 
537
544
        release_parsed_uri( uri );
538
545
      }
539
546
    else
 
547
      hd->sock = connect_server( server, port, hd->flags, hd->uri->scheme );
 
548
 
 
549
    if(auth || hd->uri->auth)
540
550
      {
541
 
        hd->sock = connect_server( server, port, hd->flags, hd->uri->scheme );
542
 
        if(hd->uri->auth)
 
551
        char *x,*tempauth=NULL;
 
552
 
 
553
        if(auth)
543
554
          {
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);
547
 
            m_free(x);
 
555
            tempauth=m_strdup(auth);
 
556
            remove_escapes(tempauth);
548
557
          }
 
558
        else if(hd->uri->auth)
 
559
          remove_escapes(hd->uri->auth);
 
560
 
 
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);
 
565
        m_free(x);
 
566
        m_free(tempauth);
549
567
      }
550
568
 
551
569
    if( hd->sock == -1 )
553
571
 
554
572
    p = build_rel_path( hd->uri );
555
573
 
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);
557
577
    if( proxy )
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:"" );
563
584
    else
564
585
      {
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:"",
575
 
                 auth?auth:"");
 
596
                 authstr?authstr:"");
576
597
      }
577
598
 
578
599
    m_free(p);
579
600
 
580
601
    rc = write_server( hd->sock, request, strlen(request) );
581
602
    m_free( request );
582
 
    m_free(auth);
 
603
    m_free(proxy_authstr);
 
604
    m_free(authstr);
583
605
 
584
606
    return rc;
585
607
}