2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
16 /* This file is responsible for implementing all direct HTTP protocol
17 channels. It is intentionally simplistic. */
32 static char six2pr[64] = {
33 'A','B','C','D','E','F','G','H','I','J','K','L','M',
34 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
35 'a','b','c','d','e','f','g','h','i','j','k','l','m',
36 'n','o','p','q','r','s','t','u','v','w','x','y','z',
37 '0','1','2','3','4','5','6','7','8','9','+','/'
41 base64_encode (char *username, char *password)
48 rv = (char *) malloc (2 * (strlen (username) + strlen (password)) + 5);
50 char *up = (char *) malloc (strlen (username) + strlen (password) + 6);
51 strcpy (up, username);
53 strcat (up, password);
54 ep = (unsigned char *)up + strlen (up);
63 for (ep = (unsigned char *)up; *ep; ep += 3)
65 block[0] = six2pr[ep[0] >> 2];
66 block[1] = six2pr[((ep[0] << 4) & 0x30) | ((ep[1] >> 4) & 0x0f)];
67 block[2] = six2pr[((ep[1] << 2) & 0x3c) | ((ep[2] >> 6) & 0x03)];
68 block[3] = six2pr[ep[2] & 0x3f];
71 block[2] = block[3] = '=';
74 memcpy (rp, block, 4);
84 NetIO_HTTP::NetIO_HTTP (char *Purl)
91 if (net_method == IDC_NET_PROXY)
92 s = new SimpleSocket (net_proxy_host, net_proxy_port);
94 s = new SimpleSocket (host, port);
102 if (net_method == IDC_NET_PROXY)
103 s->printf ("GET %s HTTP/1.0\r\n", url);
105 s->printf ("GET %s HTTP/1.0\r\n", path);
106 s->printf ("Host: %s:%d\r\n", host, port);
108 if (net_user && net_passwd)
109 s->printf ("Authorization: Basic %s\r\n",
110 base64_encode (net_user, net_passwd));
112 if (net_proxy_user && net_proxy_passwd)
113 s->printf ("Proxy-Authorization: Basic %s\r\n",
114 base64_encode (net_proxy_user, net_proxy_passwd));
118 char *l = s->gets ();
120 sscanf (l, "%*s %d", &code);
121 if (code >= 300 && code < 400)
125 if (_strnicmp (l, "Location:", 9) == 0)
128 while (*u == ' ' || *u == '\t')
136 if (code == 401) /* authorization required */
142 if (code == 407) /* proxy authorization required */
156 if (_strnicmp (l, "Content-Length:", 15) == 0)
157 sscanf (l, "%*s %d", &file_size);
161 NetIO_HTTP::~NetIO_HTTP ()
176 NetIO_HTTP::read (char *buf, int nbytes)
178 return s->read (buf, nbytes);