コード例 #1
0
ファイル: IncomingProxy.cpp プロジェクト: acadavid/proxydo
void IncomingProxy::handleConnection(ServerSocket &connection) {
  try {
    string requestLine = connection.readLine();
	requestLine = util::cleanupRequestLine(requestLine);
    string header = requestLine + "Connection: close\r\n";

	//Read header
    while (true) {
      string s = connection.readLine();
      if (s.find("Connection") == 0) continue;
      header += s;
      if (s == "\n" or s == "\r\n") break;
    }

	map<string, string> headers = util::extractHeaders(header);
	
	//Be careful, the requestLine is modified when calling findHost.
	//That is the intended behaviour.
	pair<string, int> newHost = findHost(requestLine);
	
	cout << "# [Incoming] '" << util::removeTrailingLineBreaks(requestLine)
	 						 << "' at " << newHost.first << ":" << newHost.second << endl;
	
	headers["Host"] = newHost.first;
	header = requestLine + util::assembleHeaders(headers);	

    ClientSocket webserver(newHost.first, newHost.second);
    webserver << header;
 	cout << header;

 	Socket::relay_connection(connection, webserver);

    webserver.~ClientSocket();
    connection.~ServerSocket();
  }
  catch (SocketException& e)  {
    cout << "# [Incoming] Exception: " << e.description() << endl;
  }
  catch (char * s){
    cout << "# [Incoming] Exception: " << s << endl;						
  }
}