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