2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // https-proxy - CONNECT style transparent proxy connection to SSL host.
22 // This is the CONNECT method of an ordinary (proxying) HTTP server.
23 // Once it switches a connection to transparent proxying, there's no way to get out
24 // again. Hence, our Connection objects belong to the remote host, not the proxy.
26 #include "https-proxy-protocol.h"
27 #include "netparameters.h"
35 // Construct the protocol object
37 ConnectHTTPProtocol::ConnectHTTPProtocol(Manager
&mgr
, const HostTarget
&proxy
)
38 : SecureHTTPProtocol(mgr
), host(proxy
.defaultPort(defaultHttpPort
))
44 // Create a Transfer object for our protocol
46 ConnectHTTPProtocol::ConnectHTTPTransfer
*ConnectHTTPProtocol::makeTransfer(const Target
&target
,
49 return new ConnectHTTPTransfer(*this, target
, operation
, defaultHttpsPort
);
54 // Construct an HTTPConnection object
56 ConnectHTTPProtocol::ConnectHTTPConnection::ConnectHTTPConnection(Protocol
&proto
,
57 const HostTarget
&hostTarget
)
58 : SecureHTTPConnection(proto
, hostTarget
)
61 // SecureHTTPConnection already set up everything for talking to the server
62 connectState
= connectConnecting
;
63 deferStartSSL
= true; // tell parent protocol to break on connect-complete
66 ConnectHTTPProtocol::ConnectHTTPConnection::~ConnectHTTPConnection()
72 // Start a connection request
74 void ConnectHTTPProtocol::ConnectHTTPConnection::connectRequest()
76 switch (connectState
) {
77 case connectConnecting
:
78 return; // still waiting for TCP
81 const HostTarget
&host
= target().host
;
82 flushOutput(false); // hold output
83 printfe("CONNECT %s:%d HTTP/1.1",
84 host
.host().name().c_str(), target().host
.port());
86 authorizationHeader("Proxy-Authorization", hostTarget
,
87 kNetworkGenericProxyUsername
, kNetworkGenericProxyPassword
);
88 printfe(""); // end of headers
89 flushOutput(); // flush accumulated output
91 connectState
= connectPrimaryResponse
;
94 case connectReady
: // already set; go ahead next layer (https)
98 assert(false); // huh?
104 // Our state transit method controls only the initial SSL handshake.
105 // Think of it as a "prefix" to the HTTP protocol state engine. Once the handshake
106 // is complete, we hand off further state management to the HTTP machine.
108 void ConnectHTTPProtocol::ConnectHTTPConnection::transit(Event event
,
109 char *input
, size_t inputLength
)
111 if (event
== endOfInput
&& connectState
!= connectReady
)
112 UnixError::throwMe(ECONNRESET
); // @@@ diagnostic?
114 switch (connectState
) {
115 case connectConnecting
:
116 SecureHTTPConnection::transit(event
, input
, inputLength
);
117 if (SecureHTTPConnection::sslState
== sslStartup
) { // transport level ready
118 connectState
= connectStartup
;
122 case connectPrimaryResponse
:
124 // sketchily read proxy's primary response
125 int major
, minor
, code
;
126 if (sscanf(input
, "HTTP/%d.%d %u", &major
, &minor
, &code
) != 3) {
127 fail(input
); // malformed response header
129 if (major
!= 1 || minor
< 0 || minor
> 1)
132 case 200: // okay, proceed
133 connectState
= connectReadHeaders
;
135 default: // this didn't work
136 transfer().httpResponse() = input
; // won't have a better one
141 case connectReadHeaders
:
144 headers().add(input
);
146 // end of proxy headers: start SSL now
147 connectState
= connectReady
;
150 } catch (const CssmCommonError
&err
) {
151 setError("SSL failed", err
.osStatus());
154 setError("SSL failed");
161 return SecureHTTPConnection::transit(event
, input
, inputLength
);
163 assert(false); // huh?
169 // HTTPS Transfer objects.
171 ConnectHTTPProtocol::ConnectHTTPTransfer::ConnectHTTPTransfer(Protocol
&proto
,
172 const Target
&tgt
, Operation operation
, IPPort defPort
)
173 : SecureHTTPTransfer(proto
, tgt
, operation
, defPort
)
177 void ConnectHTTPProtocol::ConnectHTTPTransfer::start()
179 ConnectHTTPConnection
*connection
=
180 protocol
.manager
.findConnection
<ConnectHTTPConnection
>(target
);
181 if (connection
== NULL
)
182 connection
= new ConnectHTTPConnection(protocol
, target
);
183 connection
->dock(this);
184 connection
->connectRequest();
189 // Even though this is formally a proxy protocol, we should not use
190 // proxy headers, since the proxy is transparent and the remote system
191 // expects a direct request.
193 bool ConnectHTTPProtocol::ConnectHTTPTransfer::useProxyHeaders() const
200 // We are a proxy protocol
202 bool ConnectHTTPProtocol::isProxy() const
205 const HostTarget
&ConnectHTTPProtocol::proxyHost() const
209 } // end namespace Network
210 } // end namespace Security