1c6149613SAdrien Destugues/*
2c6149613SAdrien Destugues * Copyright 2015 Haiku, Inc.
3c6149613SAdrien Destugues * Distributed under the terms of the MIT License.
4c6149613SAdrien Destugues */
5c6149613SAdrien Destugues
6c6149613SAdrien Destugues
7c6149613SAdrien Destugues#include <ProxySecureSocket.h>
8c6149613SAdrien Destugues
9c6149613SAdrien Destugues#include <stdio.h>
10c6149613SAdrien Destugues
11c6149613SAdrien Destugues
12c6149613SAdrien DestuguesBProxySecureSocket::BProxySecureSocket(const BNetworkAddress& proxy)
13c6149613SAdrien Destugues	:
14c6149613SAdrien Destugues	BSecureSocket(),
15c6149613SAdrien Destugues	fProxyAddress(proxy)
16c6149613SAdrien Destugues{
17c6149613SAdrien Destugues}
18c6149613SAdrien Destugues
19c6149613SAdrien Destugues
20c6149613SAdrien DestuguesBProxySecureSocket::BProxySecureSocket(const BNetworkAddress& proxy, const BNetworkAddress& peer,
21c6149613SAdrien Destugues	bigtime_t timeout)
22c6149613SAdrien Destugues	:
23c6149613SAdrien Destugues	BSecureSocket(),
24c6149613SAdrien Destugues	fProxyAddress(proxy)
25c6149613SAdrien Destugues{
26c6149613SAdrien Destugues	Connect(peer, timeout);
27c6149613SAdrien Destugues}
28c6149613SAdrien Destugues
29c6149613SAdrien Destugues
30c6149613SAdrien DestuguesBProxySecureSocket::BProxySecureSocket(const BProxySecureSocket& other)
31c6149613SAdrien Destugues	:
32c6149613SAdrien Destugues	BSecureSocket(other),
33c6149613SAdrien Destugues	fProxyAddress(other.fProxyAddress)
34c6149613SAdrien Destugues{
35c6149613SAdrien Destugues}
36c6149613SAdrien Destugues
37c6149613SAdrien Destugues
38c6149613SAdrien DestuguesBProxySecureSocket::~BProxySecureSocket()
39c6149613SAdrien Destugues{
40c6149613SAdrien Destugues}
41c6149613SAdrien Destugues
42c6149613SAdrien Destugues
43c6149613SAdrien Destuguesstatus_t
44c6149613SAdrien DestuguesBProxySecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout)
45c6149613SAdrien Destugues{
46c6149613SAdrien Destugues	status_t status = InitCheck();
47c6149613SAdrien Destugues	if (status != B_OK)
48c6149613SAdrien Destugues		return status;
49c6149613SAdrien Destugues
5075fa008eSAugustin Cavalier	status = BSocket::Connect(fProxyAddress, timeout);
51c6149613SAdrien Destugues	if (status != B_OK)
52c6149613SAdrien Destugues		return status;
53c6149613SAdrien Destugues
54c6149613SAdrien Destugues	BString connectRequest;
55c6149613SAdrien Destugues	connectRequest.SetToFormat("CONNECT %s:%d HTTP/1.0\r\n\r\n",
56c6149613SAdrien Destugues		peer.HostName().String(), peer.Port());
57c6149613SAdrien Destugues	BSocket::Write(connectRequest.String(), connectRequest.Length());
58c6149613SAdrien Destugues
59c6149613SAdrien Destugues	char buffer[256];
60c6149613SAdrien Destugues	ssize_t length = BSocket::Read(buffer, sizeof(buffer) - 1);
61c6149613SAdrien Destugues	if (length <= 0)
62c6149613SAdrien Destugues		return length;
63c6149613SAdrien Destugues
64c6149613SAdrien Destugues	buffer[length] = '\0';
65c6149613SAdrien Destugues	int httpStatus = 0;
6675fa008eSAugustin Cavalier	int matches = sscanf(buffer, "HTTP/1.0 %d %*[^\r\n]\r\n\r\n", &httpStatus);
67c6149613SAdrien Destugues	if (matches != 2)
68c6149613SAdrien Destugues		return B_BAD_DATA;
69c6149613SAdrien Destugues
70c6149613SAdrien Destugues	if (httpStatus < 200 || httpStatus > 299)
71c6149613SAdrien Destugues		return B_BAD_VALUE;
72c6149613SAdrien Destugues
73c9dd7d0dSRene Gollent	return _SetupConnect();
74c6149613SAdrien Destugues}
75c6149613SAdrien Destugues
76c6149613SAdrien Destugues
77