comparison src/proxy.c @ 1843:4dbd8533d209

[gaim-migrate @ 1853] http can be authenticated committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sat, 12 May 2001 08:09:54 +0000
parents 00aef397a1fe
children 9845deede1e9
comparison
equal deleted inserted replaced
1842:1d137dde946f 1843:4dbd8533d209
43 struct PHB { 43 struct PHB {
44 GdkInputFunction func; 44 GdkInputFunction func;
45 gpointer data; 45 gpointer data;
46 char *host; 46 char *host;
47 int port; 47 int port;
48 char *user;
49 char *pass;
48 gint inpa; 50 gint inpa;
49 }; 51 };
50 52
51 static void no_one_calls(gpointer data, gint source, GdkInputCondition cond) 53 static void no_one_calls(gpointer data, gint source, GdkInputCondition cond)
52 { 54 {
140 debug_printf("Proxy says: %s\n", inputline); 142 debug_printf("Proxy says: %s\n", inputline);
141 143
142 if ((memcmp(HTTP_GOODSTRING , inputline, strlen(HTTP_GOODSTRING )) == 0) || 144 if ((memcmp(HTTP_GOODSTRING , inputline, strlen(HTTP_GOODSTRING )) == 0) ||
143 (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { 145 (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
144 phb->func(phb->data, source, GDK_INPUT_READ); 146 phb->func(phb->data, source, GDK_INPUT_READ);
147 if (phb->user) {
148 g_free(phb->user);
149 g_free(phb->pass);
150 }
145 g_free(phb->host); 151 g_free(phb->host);
146 g_free(phb); 152 g_free(phb);
147 return; 153 return;
148 } 154 }
149 155
150 close(source); 156 close(source);
151 phb->func(phb->data, -1, GDK_INPUT_READ); 157 phb->func(phb->data, -1, GDK_INPUT_READ);
158 if (phb->user) {
159 g_free(phb->user);
160 g_free(phb->pass);
161 }
152 g_free(phb->host); 162 g_free(phb->host);
153 g_free(phb); 163 g_free(phb);
154 return; 164 return;
155 } 165 }
156 166
164 gdk_input_remove(phb->inpa); 174 gdk_input_remove(phb->inpa);
165 len = sizeof(error); 175 len = sizeof(error);
166 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { 176 if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
167 close(source); 177 close(source);
168 phb->func(phb->data, -1, GDK_INPUT_READ); 178 phb->func(phb->data, -1, GDK_INPUT_READ);
179 if (phb->user) {
180 g_free(phb->user);
181 g_free(phb->pass);
182 }
169 g_free(phb->host); 183 g_free(phb->host);
170 g_free(phb); 184 g_free(phb);
171 return; 185 return;
172 } 186 }
173 fcntl(source, F_SETFL, 0); 187 fcntl(source, F_SETFL, 0);
174 188
175 snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\n\r\n", phb->host, phb->port); 189 g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\n", phb->host, phb->port);
176 if (send(source, cmd, strlen(cmd), 0) < 0) { 190 if (send(source, cmd, strlen(cmd), 0) < 0) {
177 close(source); 191 close(source);
178 phb->func(phb->data, -1, GDK_INPUT_READ); 192 phb->func(phb->data, -1, GDK_INPUT_READ);
193 if (phb->user) {
194 g_free(phb->user);
195 g_free(phb->pass);
196 }
197 g_free(phb->host);
198 g_free(phb);
199 return;
200 }
201
202 if (phb->user) {
203 char *t1, *t2;
204 t1 = g_strdup_printf("%s:%s", phb->user, phb->pass);
205 t2 = tobase64(t1);
206 g_free(t1);
207 g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
208 g_free(t2);
209 if (send(source, cmd, strlen(cmd), 0) < 0) {
210 close(source);
211 phb->func(phb->data, -1, GDK_INPUT_READ);
212 g_free(phb->user);
213 g_free(phb->pass);
214 g_free(phb->host);
215 g_free(phb);
216 return;
217 }
218 }
219
220 g_snprintf(cmd, sizeof(cmd), "\r\n");
221 if (send(source, cmd, strlen(cmd), 0) < 0) {
222 close(source);
223 phb->func(phb->data, -1, GDK_INPUT_READ);
224 if (phb->user) {
225 g_free(phb->user);
226 g_free(phb->pass);
227 }
179 g_free(phb->host); 228 g_free(phb->host);
180 g_free(phb); 229 g_free(phb);
181 return; 230 return;
182 } 231 }
183 232
184 phb->inpa = gdk_input_add(source, GDK_INPUT_READ, http_canread, phb); 233 phb->inpa = gdk_input_add(source, GDK_INPUT_READ, http_canread, phb);
185 } 234 }
186 235
187 static int proxy_connect_http(char *host, unsigned short port, 236 static int proxy_connect_http(char *host, unsigned short port,
188 char *proxyhost, unsigned short proxyport, 237 char *proxyhost, unsigned short proxyport,
238 char *user, char *pass,
189 struct PHB *phb) 239 struct PHB *phb)
190 { 240 {
191 struct hostent *hp; 241 struct hostent *hp;
192 struct sockaddr_in sin; 242 struct sockaddr_in sin;
193 int fd = -1; 243 int fd = -1;
209 return -1; 259 return -1;
210 } 260 }
211 261
212 phb->host = g_strdup(host); 262 phb->host = g_strdup(host);
213 phb->port = port; 263 phb->port = port;
264 if (user && pass && user[0] && pass[0]) {
265 phb->user = g_strdup(user);
266 phb->pass = g_strdup(pass);
267 }
214 268
215 fcntl(fd, F_SETFL, O_NONBLOCK); 269 fcntl(fd, F_SETFL, O_NONBLOCK);
216 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 270 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
217 if ((errno == EINPROGRESS) || (errno == EINTR)) { 271 if ((errno == EINPROGRESS) || (errno == EINTR)) {
218 debug_printf("Connect would have blocked\n"); 272 debug_printf("Connect would have blocked\n");
219 phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, http_canwrite, phb); 273 phb->inpa = gdk_input_add(fd, GDK_INPUT_WRITE, http_canwrite, phb);
220 } else { 274 } else {
221 close(fd); 275 close(fd);
276 if (phb->user) {
277 g_free(phb->user);
278 g_free(phb->pass);
279 }
222 g_free(phb->host); 280 g_free(phb->host);
223 g_free(phb); 281 g_free(phb);
224 return -1; 282 return -1;
225 } 283 }
226 } else { 284 } else {
227 int len, error = ETIMEDOUT; 285 int len, error = ETIMEDOUT;
228 debug_printf("Connect didn't block\n"); 286 debug_printf("Connect didn't block\n");
229 len = sizeof(error); 287 len = sizeof(error);
230 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { 288 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
231 close(fd); 289 close(fd);
290 if (phb->user) {
291 g_free(phb->user);
292 g_free(phb->pass);
293 }
232 g_free(phb->host); 294 g_free(phb->host);
233 g_free(phb); 295 g_free(phb);
234 return -1; 296 return -1;
235 } 297 }
236 fcntl(fd, F_SETFL, 0); 298 fcntl(fd, F_SETFL, 0);
534 return fd; 596 return fd;
535 } 597 }
536 598
537 int proxy_connect(char *host, int port, 599 int proxy_connect(char *host, int port,
538 char *proxyhost, int proxyport, int proxytype, 600 char *proxyhost, int proxyport, int proxytype,
601 char *user, char *pass,
539 GdkInputFunction func, gpointer data) 602 GdkInputFunction func, gpointer data)
540 { 603 {
541 struct PHB *phb = g_new0(struct PHB, 1); 604 struct PHB *phb = g_new0(struct PHB, 1);
542 phb->func = func; 605 phb->func = func;
543 phb->data = data; 606 phb->data = data;
550 if ((proxytype == PROXY_NONE) || 613 if ((proxytype == PROXY_NONE) ||
551 !proxyhost || !proxyhost[0] || 614 !proxyhost || !proxyhost[0] ||
552 !proxyport || (proxyport == -1)) 615 !proxyport || (proxyport == -1))
553 return proxy_connect_none(host, port, phb); 616 return proxy_connect_none(host, port, phb);
554 else if (proxytype == PROXY_HTTP) 617 else if (proxytype == PROXY_HTTP)
555 return proxy_connect_http(host, port, proxyhost, proxyport, phb); 618 return proxy_connect_http(host, port, proxyhost, proxyport, user, pass, phb);
556 else if (proxytype == PROXY_SOCKS4) 619 else if (proxytype == PROXY_SOCKS4)
557 return proxy_connect_socks4(host, port, proxyhost, proxyport, phb); 620 return proxy_connect_socks4(host, port, proxyhost, proxyport, phb);
558 else if (proxytype == PROXY_SOCKS5) 621 else if (proxytype == PROXY_SOCKS5)
559 return proxy_connect_socks5(host, port, proxyhost, proxyport, phb); 622 return proxy_connect_socks5(host, port, proxyhost, proxyport, phb);
560 623