]> cygwin.com Git - cygwin-apps/setup.git/blob - nio-http.cc
2002-01-27 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / nio-http.cc
1 /*
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
3 *
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.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* This file is responsible for implementing all direct HTTP protocol
17 channels. It is intentionally simplistic. */
18
19 #if 0
20 static const char *cvsid =
21 "\n%%% $Id$\n";
22 #endif
23
24 #include "win32.h"
25 #include "winsock.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "resource.h"
30 #include "state.h"
31 #include "simpsock.h"
32 #include "msg.h"
33 #include "concat.h"
34
35 #include "netio.h"
36 #include "nio-http.h"
37
38 static char six2pr[64] = {
39 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
40 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
41 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
42 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
43 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
44 };
45
46 static char *
47 base64_encode (char *username, char *password)
48 {
49 unsigned char *ep;
50 char *rp;
51 static char *rv = 0;
52 if (rv)
53 delete[] rv;
54 rv = new char[2 * (strlen (username) + strlen (password)) + 5];
55
56 char *up = new char[strlen (username) + strlen (password) + 6];
57 strcpy (up, username);
58 strcat (up, ":");
59 strcat (up, password);
60 ep = (unsigned char *) up + strlen (up);
61 *ep++ = 0;
62 *ep++ = 0;
63 *ep++ = 0;
64
65 char block[4];
66
67 rp = rv;
68
69 for (ep = (unsigned char *) up; *ep; ep += 3)
70 {
71 block[0] = six2pr[ep[0] >> 2];
72 block[1] = six2pr[((ep[0] << 4) & 0x30) | ((ep[1] >> 4) & 0x0f)];
73 block[2] = six2pr[((ep[1] << 2) & 0x3c) | ((ep[2] >> 6) & 0x03)];
74 block[3] = six2pr[ep[2] & 0x3f];
75
76 if (ep[1] == 0)
77 block[2] = block[3] = '=';
78 if (ep[2] == 0)
79 block[3] = '=';
80 memcpy (rp, block, 4);
81 rp += 4;
82 }
83 *rp = 0;
84
85 delete[] up;
86
87 return rv;
88 }
89
90 NetIO_HTTP::NetIO_HTTP (char const *Purl, BOOL allow_ftp_auth):NetIO (Purl, allow_ftp_auth)
91 {
92 retry_get:
93 if (port == 0)
94 port = 80;
95
96 if (net_method == IDC_NET_PROXY)
97 s = new SimpleSocket (net_proxy_host, net_proxy_port);
98 else
99 s = new SimpleSocket (host, port);
100
101 if (!s->ok ())
102 {
103 delete
104 s;
105 s = NULL;
106 return;
107 }
108
109 if (net_method == IDC_NET_PROXY)
110 s->printf ("GET %s HTTP/1.0\r\n", url);
111 else
112 s->printf ("GET %s HTTP/1.0\r\n", path);
113 s->printf ("Host: %s:%d\r\n", host, port);
114
115 if (net_user && net_passwd)
116 s->printf ("Authorization: Basic %s\r\n",
117 base64_encode (net_user, net_passwd));
118
119 if (net_proxy_user && net_proxy_passwd)
120 s->printf ("Proxy-Authorization: Basic %s\r\n",
121 base64_encode (net_proxy_user, net_proxy_passwd));
122
123 s->printf ("\r\n");
124
125 char *
126 l = s->gets ();
127 int
128 code;
129 if (!l)
130 return;
131 sscanf (l, "%*s %d", &code);
132 if (code >= 300 && code < 400)
133 {
134 while ((l = s->gets ()) != 0)
135 {
136 if (_strnicmp (l, "Location:", 9) == 0)
137 {
138 char *
139 u = l + 9;
140 while (*u == ' ' || *u == '\t')
141 u++;
142 set_url (u);
143 delete
144 s;
145 goto retry_get;
146 }
147 }
148 }
149 if (code == 401) /* authorization required */
150 {
151 get_auth (NULL);
152 delete
153 s;
154 goto retry_get;
155 }
156 if (code == 407) /* proxy authorization required */
157 {
158 get_proxy_auth (NULL);
159 delete
160 s;
161 goto retry_get;
162 }
163 if (code == 500 /* ftp authentication through proxy required */
164 && net_method == IDC_NET_PROXY && !strncmp (url, "ftp://", 6))
165 {
166 get_ftp_auth (NULL);
167 if (net_ftp_user && net_ftp_passwd)
168 {
169 delete
170 s;
171 url = concat ("ftp://", net_ftp_user,
172 ":", net_ftp_passwd, "@", url + 6, 0);
173 goto retry_get;
174 }
175 }
176 if (code >= 300)
177 {
178 delete
179 s;
180 s = 0;
181 return;
182 }
183
184 // Eat the header, picking out the Content-Length in the process
185 while (((l = s->gets ()) != NULL) && (*l != '\0'))
186 {
187 if (_strnicmp (l, "Content-Length:", 15) == 0)
188 sscanf (l, "%*s %d", &file_size);
189 }
190 }
191
192 NetIO_HTTP::~NetIO_HTTP ()
193 {
194 if (s)
195 delete s;
196 }
197
198 int
199 NetIO_HTTP::ok ()
200 {
201 if (s && s->ok ())
202 return 1;
203 return 0;
204 }
205
206 int
207 NetIO_HTTP::read (char *buf, int nbytes)
208 {
209 return s->read (buf, nbytes);
210 }
This page took 0.045984 seconds and 5 git commands to generate.