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