]> cygwin.com Git - cygwin-apps/setup.git/blob - nio-http.cc
* CHANGES: Update.
[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
34 #include "netio.h"
35 #include "nio-http.h"
36
37 #ifndef _strnicmp
38 #define _strnicmp strncasecmp
39 #endif
40
41 static char six2pr[64] = {
42 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
43 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
44 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
45 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
46 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
47 };
48
49 static char *
50 base64_encode (char *username, char *password)
51 {
52 unsigned char *ep;
53 char *rp;
54 static char *rv = 0;
55 if (rv)
56 delete[] rv;
57 rv = new char[2 * (strlen (username) + strlen (password)) + 5];
58
59 char *up = new char[strlen (username) + strlen (password) + 6];
60 strcpy (up, username);
61 strcat (up, ":");
62 strcat (up, password);
63 ep = (unsigned char *) up + strlen (up);
64 *ep++ = 0;
65 *ep++ = 0;
66 *ep++ = 0;
67
68 char block[4];
69
70 rp = rv;
71
72 for (ep = (unsigned char *) up; *ep; ep += 3)
73 {
74 block[0] = six2pr[ep[0] >> 2];
75 block[1] = six2pr[((ep[0] << 4) & 0x30) | ((ep[1] >> 4) & 0x0f)];
76 block[2] = six2pr[((ep[1] << 2) & 0x3c) | ((ep[2] >> 6) & 0x03)];
77 block[3] = six2pr[ep[2] & 0x3f];
78
79 if (ep[1] == 0)
80 block[2] = block[3] = '=';
81 if (ep[2] == 0)
82 block[3] = '=';
83 memcpy (rp, block, 4);
84 rp += 4;
85 }
86 *rp = 0;
87
88 delete[] up;
89
90 return rv;
91 }
92
93 NetIO_HTTP::NetIO_HTTP (char const *Purl):NetIO (Purl)
94 {
95 retry_get:
96 if (port == 0)
97 port = 80;
98
99 if (net_method == IDC_NET_PROXY)
100 s = new SimpleSocket (net_proxy_host, net_proxy_port);
101 else
102 s = new SimpleSocket (host, port);
103
104 if (!s->ok ())
105 {
106 delete
107 s;
108 s = NULL;
109 return;
110 }
111
112 if (net_method == IDC_NET_PROXY)
113 s->printf ("GET %s HTTP/1.0\r\n", Purl);
114 else
115 s->printf ("GET %s HTTP/1.0\r\n", path);
116
117 // Default HTTP port is 80. Host header can have no port if requested port
118 // is the same as the default. Some HTTP servers don't behave as expected
119 // when they receive a Host header with the unnecessary default port value.
120 if (port == 80)
121 s->printf ("Host: %s\r\n", host);
122 else
123 s->printf ("Host: %s:%d\r\n", host, port);
124
125 if (net_user && net_passwd)
126 s->printf ("Authorization: Basic %s\r\n",
127 base64_encode (net_user, net_passwd));
128
129 if (net_proxy_user && net_proxy_passwd)
130 s->printf ("Proxy-Authorization: Basic %s\r\n",
131 base64_encode (net_proxy_user, net_proxy_passwd));
132
133 s->printf ("\r\n");
134
135 char *
136 l = s->gets ();
137 int
138 code;
139 if (!l)
140 return;
141 sscanf (l, "%*s %d", &code);
142 if (code >= 300 && code < 400)
143 {
144 while ((l = s->gets ()) != 0)
145 {
146 if (_strnicmp (l, "Location:", 9) == 0)
147 {
148 char *
149 u = l + 9;
150 while (*u == ' ' || *u == '\t')
151 u++;
152 set_url (u);
153 delete
154 s;
155 goto retry_get;
156 }
157 }
158 }
159 if (code == 401) /* authorization required */
160 {
161 get_auth (NULL);
162 delete
163 s;
164 goto retry_get;
165 }
166 if (code == 407) /* proxy authorization required */
167 {
168 get_proxy_auth (NULL);
169 delete
170 s;
171 goto retry_get;
172 }
173 if (code == 500 /* ftp authentication through proxy required */
174 && net_method == IDC_NET_PROXY && !strncmp (Purl, "ftp://", 6))
175 {
176 get_ftp_auth (NULL);
177 if (net_ftp_user && net_ftp_passwd)
178 {
179 delete
180 s;
181 Purl = (std::string("ftp://") + net_ftp_user +
182 ":" + net_ftp_passwd + "@" + (Purl + 6)).c_str();
183 goto retry_get;
184 }
185 }
186 if (code >= 300)
187 {
188 delete
189 s;
190 s = 0;
191 return;
192 }
193
194 // Eat the header, picking out the Content-Length in the process
195 while (((l = s->gets ()) != NULL) && (*l != '\0'))
196 {
197 if (_strnicmp (l, "Content-Length:", 15) == 0)
198 sscanf (l, "%*s %d", &file_size);
199 }
200 }
201
202 NetIO_HTTP::~NetIO_HTTP ()
203 {
204 if (s)
205 delete s;
206 }
207
208 int
209 NetIO_HTTP::ok ()
210 {
211 if (s && s->ok ())
212 return 1;
213 return 0;
214 }
215
216 int
217 NetIO_HTTP::read (char *buf, int nbytes)
218 {
219 return s->read (buf, nbytes);
220 }
This page took 0.044786 seconds and 5 git commands to generate.