Skip to content

Instantly share code, notes, and snippets.

@apage43
Created December 7, 2016 02:26
Show Gist options
  • Save apage43/271f82d3317b4dfcfebd04a3cc8647f2 to your computer and use it in GitHub Desktop.
Save apage43/271f82d3317b4dfcfebd04a3cc8647f2 to your computer and use it in GitHub Desktop.
diff --git a/palaver.cpp b/palaver.cpp
--- a/palaver.cpp
+++ b/palaver.cpp
@@ -64,10 +66,12 @@ CString re_escape(const CString& sString) {
typedef enum {
- StatusLine = 0,
- Headers = 1,
- Body = 2,
- Closed = 3,
+ ProxyStatusLine = 0,
+ ProxyHeaders = 1,
+ StatusLine = 2,
+ Headers = 3,
+ Body = 4,
+ Closed = 5,
} EPLVHTTPSocketState;
class PLVHTTPSocket : public CSocket {
@@ -75,7 +79,6 @@ class PLVHTTPSocket : public CSocket {
public:
PLVHTTPSocket(CModule *pModule, const CString &sMethod, const CString &sURL, MCString &mcsHeaders, const CString &sContent) : CSocket(pModule) {
- m_eState = StatusLine;
unsigned short uPort = 80;
@@ -104,26 +107,29 @@ public:
mcsHeaders["Content-Length"] = CString(sContent.length());
}
- bool useSSL = sScheme.Equals("https");
+ m_useSSL = sScheme.Equals("https");
- DEBUG("Palaver: Connecting to '" << m_sHostname << "' on port " << uPort << (useSSL ? " with" : " without") << " TLS (" << sMethod << " " << sPath << ")");
+ DEBUG("Palaver: Connecting to '" << m_sHostname << "' on port " << uPort << (m_useSSL ? " with" : " without") << " TLS (" << sMethod << " " << sPath << ")");
- Connect(m_sHostname, uPort, useSSL);
+ //Connect(m_sHostname, uPort, useSSL);
+ m_eState = ProxyStatusLine;
+ Connect("proxyhost", 8080, false);
+ Write("CONNECT " + m_sHostname + ":" + CString(uPort) + " HTTP/1.1\r\n\r\n");
EnableReadLine();
- Write(sMethod + " " + sPath + " HTTP/1.1\r\n");
- Write("Host: " + m_sHostname + "\r\n");
+ m_sPrepRequest += sMethod + " " + sPath + " HTTP/1.1\r\n";
+ m_sPrepRequest += "Host: " + m_sHostname + "\r\n";
for (MCString::const_iterator it = mcsHeaders.begin(); it != mcsHeaders.end(); ++it) {
const CString &sKey = it->first;
const CString &sValue = it->second;
- Write(sKey + ": " + sValue + "\r\n");
+ m_sPrepRequest += sKey + ": " + sValue + "\r\n";
}
- Write("\r\n");
+ m_sPrepRequest += "\r\n";
if (sContent.length() > 0) {
- Write(sContent);
+ m_sPrepRequest += sContent;
}
}
@@ -136,6 +142,35 @@ public:
sLine.TrimRight("\r\n");
switch (m_eState) {
+ case ProxyStatusLine: {
+ CString sStatus = sLine.Token(1);
+ unsigned int uStatus = sStatus.ToUInt();
+
+ if (uStatus < 200 || uStatus > 299) {
+ DEBUG("Palaver: Received HTTP Response code: " << uStatus);
+ } else {
+ DEBUG("Palaver: Successfully connected thru proxy ('" << uStatus << "')");
+ }
+
+ m_eState = ProxyHeaders;
+ break;
+ }
+
+ case ProxyHeaders: {
+ DEBUG("Proxy header: " << sLine);
+ if (sLine.empty()) {
+ DEBUG("End of proxy headers.");
+ if (m_useSSL) {
+ bool res = StartTLS();
+ DEBUG("Start TLS in proxied conn: " << res);
+ }
+ DEBUG("Sending prepared request.");
+ Write(m_sPrepRequest);
+ m_eState = StatusLine;
+ }
+ break;
+ }
+
case StatusLine: {
CString sStatus = sLine.Token(1);
unsigned int uStatus = sStatus.ToUInt();
@@ -194,6 +229,8 @@ public:
}
private:
+ bool m_useSSL;
+ CString m_sPrepRequest;
CString m_sHostname;
};
--
2.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment