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