]>
Commit | Line | Data |
---|---|---|
4a83b7b0 | 1 | /* |
85553593 | 2 | * Copyright (c) 2000, 2001, Red Hat, Inc. |
4a83b7b0 DD |
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 | ||
5898e300 | 19 | #if 0 |
b24c88b3 RC |
20 | static const char *cvsid = |
21 | "\n%%% $Id$\n"; | |
5898e300 | 22 | #endif |
946fc045 | 23 | |
4a83b7b0 DD |
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" | |
85553593 | 33 | #include "concat.h" |
4a83b7b0 DD |
34 | |
35 | #include "netio.h" | |
36 | #include "nio-http.h" | |
37 | ||
38 | static char six2pr[64] = { | |
b24c88b3 RC |
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', '+', '/' | |
4a83b7b0 DD |
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 | free (rv); | |
54 | rv = (char *) malloc (2 * (strlen (username) + strlen (password)) + 5); | |
55 | ||
56 | char *up = (char *) malloc (strlen (username) + strlen (password) + 6); | |
57 | strcpy (up, username); | |
58 | strcat (up, ":"); | |
59 | strcat (up, password); | |
b24c88b3 | 60 | ep = (unsigned char *) up + strlen (up); |
4a83b7b0 DD |
61 | *ep++ = 0; |
62 | *ep++ = 0; | |
63 | *ep++ = 0; | |
64 | ||
65 | char block[4]; | |
66 | ||
67 | rp = rv; | |
68 | ||
b24c88b3 | 69 | for (ep = (unsigned char *) up; *ep; ep += 3) |
4a83b7b0 DD |
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 | free (up); | |
86 | ||
87 | return rv; | |
88 | } | |
89 | ||
b24c88b3 | 90 | NetIO_HTTP::NetIO_HTTP (char *Purl, BOOL allow_ftp_auth):NetIO (Purl, allow_ftp_auth) |
4a83b7b0 | 91 | { |
b24c88b3 | 92 | retry_get: |
4a83b7b0 DD |
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 | ||
b24c88b3 | 101 | if (!s->ok ()) |
4a83b7b0 | 102 | { |
b24c88b3 RC |
103 | delete |
104 | s; | |
c168185f | 105 | s = NULL; |
4a83b7b0 DD |
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 | ||
b24c88b3 RC |
125 | char * |
126 | l = s->gets (); | |
127 | int | |
128 | code; | |
67a55ad9 RC |
129 | if (!l) |
130 | return; | |
4a83b7b0 DD |
131 | sscanf (l, "%*s %d", &code); |
132 | if (code >= 300 && code < 400) | |
133 | { | |
b24c88b3 RC |
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 | } | |
4a83b7b0 | 148 | } |
b24c88b3 | 149 | if (code == 401) /* authorization required */ |
4a83b7b0 DD |
150 | { |
151 | get_auth (); | |
b24c88b3 RC |
152 | delete |
153 | s; | |
4a83b7b0 DD |
154 | goto retry_get; |
155 | } | |
b24c88b3 | 156 | if (code == 407) /* proxy authorization required */ |
4a83b7b0 DD |
157 | { |
158 | get_proxy_auth (); | |
b24c88b3 RC |
159 | delete |
160 | s; | |
4a83b7b0 DD |
161 | goto retry_get; |
162 | } | |
b24c88b3 RC |
163 | if (code == 500 /* ftp authentication through proxy required */ |
164 | && net_method == IDC_NET_PROXY && !strncmp (url, "ftp://", 6)) | |
85553593 CV |
165 | { |
166 | get_ftp_auth (); | |
167 | if (net_ftp_user && net_ftp_passwd) | |
b24c88b3 RC |
168 | { |
169 | delete | |
170 | s; | |
85553593 | 171 | url = concat ("ftp://", net_ftp_user, |
b24c88b3 RC |
172 | ":", net_ftp_passwd, "@", url + 6, 0); |
173 | goto retry_get; | |
85553593 CV |
174 | } |
175 | } | |
4a83b7b0 DD |
176 | if (code >= 300) |
177 | { | |
b24c88b3 RC |
178 | delete |
179 | s; | |
4a83b7b0 DD |
180 | s = 0; |
181 | return; | |
182 | } | |
b24c88b3 RC |
183 | while ((l = s->gets ()) != 0) |
184 | { | |
185 | if (_strnicmp (l, "Content-Length:", 15) == 0) | |
186 | sscanf (l, "%*s %d", &file_size); | |
187 | } | |
4a83b7b0 DD |
188 | } |
189 | ||
190 | NetIO_HTTP::~NetIO_HTTP () | |
191 | { | |
192 | if (s) | |
193 | delete s; | |
194 | } | |
195 | ||
196 | int | |
197 | NetIO_HTTP::ok () | |
198 | { | |
67a55ad9 | 199 | if (s && s->ok ()) |
4a83b7b0 DD |
200 | return 1; |
201 | return 0; | |
202 | } | |
203 | ||
204 | int | |
205 | NetIO_HTTP::read (char *buf, int nbytes) | |
206 | { | |
207 | return s->read (buf, nbytes); | |
208 | } |