File: proxy_http.c

package info (click to toggle)
ibm-3270 4.0ga12-3
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 17,740 kB
  • sloc: ansic: 120,111; sh: 4,284; makefile: 822; pascal: 798; perl: 344; exp: 184; tcl: 94
file content (220 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2007-2009, 2013-2015, 2018-2019 Paul Mattes.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the names of Paul Mattes nor the names of his contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *	proxy_http.c
 *		RFC 2817 HTTP CONNECT tunnel proxy.
 */

#include "globals.h"

#if !defined(_WIN32) /*[*/
# include <errno.h>
# include <sys/ioctl.h>
# include <netinet/in.h>
# include <netdb.h>
# include <arpa/inet.h>
#endif /*]*/

#include "base64.h"
#include "lazya.h"
#include "popups.h"
#include "proxy.h"
#include "proxy_private.h"
#include "proxy_http.h"
#include "resolver.h"
#include "telnet_core.h"
#include "trace.h"
#include "utils.h"
#include "w3misc.h"

#define RBUF	1024

/* Proxy state. */
static struct {
    socket_t fd;
    unsigned char *rbuf;
    size_t nread;
} ps = { INVALID_SOCKET, NULL, 0 };

/* HTTP (RFC 2817 CONNECT tunnel) proxy. */
proxy_negotiate_ret_t
proxy_http(socket_t fd, const char *user, const char *host, unsigned short port)
{
    char *sbuf;
    char *colon;

    ps.fd = fd;
    ps.rbuf = Malloc(RBUF);
    ps.nread = 0;

    /* Send the CONNECT request. */
    colon = strchr(host, ':');
    sbuf = xs_buffer("CONNECT %s%s%s:%u HTTP/1.1\r\n",
	    (colon? "[": ""),
	    host,
	    (colon? "]": ""),
	    port);

    vtrace("HTTP Proxy: xmit '%.*s'\n", (int)(strlen(sbuf) - 2), sbuf);
    trace_netdata('>', (unsigned char *)sbuf, strlen(sbuf));

    if (send(fd, sbuf, (int)strlen(sbuf), 0) < 0) {
	popup_a_sockerr("HTTP Proxy: send error");
	Free(sbuf);
	return PX_FAILURE;
    }

    Free(sbuf);
    sbuf = xs_buffer("Host: %s%s%s:%u\r\n",
	    (colon? "[": ""),
	    host,
	    (colon? "]": ""),
	    port);

    vtrace("HTTP Proxy: xmit '%.*s'\n", (int)(strlen(sbuf) - 2), sbuf);
    trace_netdata('>', (unsigned char *)sbuf, strlen(sbuf));

    if (send(fd, sbuf, (int)strlen(sbuf), 0) < 0) {
	popup_a_sockerr("HTTP Proxy: send error");
	Free(sbuf);
	return PX_FAILURE;
    }

    if (user != NULL) {
	Free(sbuf);
	sbuf = xs_buffer("Proxy-Authorization: Basic %s\r\n",
		lazya(base64_encode(user)));

	vtrace("HTTP Proxy: xmit '%.*s'\n", (int)(strlen(sbuf) - 2), sbuf);
	trace_netdata('>', (unsigned char *)sbuf, strlen(sbuf));

	if (send(fd, sbuf, (int)strlen(sbuf), 0) < 0) {
	    popup_a_sockerr("HTTP Proxy: send error");
	    Free(sbuf);
	    return PX_FAILURE;
	}
    }

    Free(sbuf);
    sbuf = "\r\n";
    vtrace("HTTP Proxy: xmit ''\n");
    trace_netdata('>', (unsigned char *)sbuf, strlen(sbuf));

    if (send(fd, sbuf, (int)strlen(sbuf), 0) < 0) {
	popup_a_sockerr("HTTP Proxy: send error");
	return PX_FAILURE;
    }

    return PX_WANTMORE;
}

/* HTTP proxy continuation. */
proxy_negotiate_ret_t
proxy_http_continue(void)
{
    char *space;
    bool nl = false;

    /*
     * Process the reply.
     * Read a byte at a time until two \n or EOF.
     */
    for (;;) {
	ssize_t nr = recv(ps.fd, (char *)&ps.rbuf[ps.nread], 1, 0);
	if (nr < 0) {
	    if (socket_errno() == SE_EWOULDBLOCK) {
		if (ps.nread) {
		    trace_netdata('<', ps.rbuf, ps.nread);
		}
		return PX_WANTMORE;
	    }
	    popup_a_sockerr("HTTP Proxy: receive error");
	    if (ps.nread) {
		trace_netdata('<', ps.rbuf, ps.nread);
	    }
	    return PX_FAILURE;
	}
	if (nr == 0) {
	    if (ps.nread) {
		trace_netdata('<', ps.rbuf, ps.nread);
	    }
	    popup_an_error("HTTP Proxy: unexpected EOF");
	    return PX_FAILURE;
	}
	if (++ps.nread >= RBUF) {
	    ps.nread = RBUF - 1;
	    break;
	}
	if (ps.nread && ps.rbuf[ps.nread - 1] == '\n') {
	    if (nl) {
		break;
	    }
	    nl = true;
	}
    }

    trace_netdata('<', ps.rbuf, ps.nread);
    if (ps.rbuf[ps.nread - 1] == '\n') {
	--ps.nread;
    }
    if (ps.rbuf[ps.nread - 1] == '\r') {
	--ps.nread;
    }
    if (ps.rbuf[ps.nread - 1] == '\n') {
	--ps.nread;
    }
    if (ps.rbuf[ps.nread - 1] == '\r') {
	--ps.nread;
    }
    ps.rbuf[ps.nread] = '\0';
    vtrace("HTTP Proxy: recv '%s'\n", (char *)ps.rbuf);

    if (strncmp((char *)ps.rbuf, "HTTP/", 5) ||
	    (space = strchr((char *)ps.rbuf, ' ')) == NULL) {
	popup_an_error("HTTP Proxy: unrecognized reply");
	return PX_FAILURE;
    }
    if (*(space + 1) != '2') {
	popup_an_error("HTTP Proxy: CONNECT failed:\n%s",
		(char *)ps.rbuf);
	return PX_FAILURE;
    }

    return PX_SUCCESS;
}

/*
 * Close the HTTP proxy.
 */
void
proxy_http_close(void)
{
    ps.fd = INVALID_SOCKET;
    Replace(ps.rbuf, NULL);
    ps.nread = 0;
}