HttpProxy::HttpProxy(int localport, const char* url, FILE* ofile, const char* url_prefix) : m_url_prefix(url_prefix) { m_localport = localport; if(url && *url) { StringBuffer protocol, username, password, portbuf, path; Http::SplitURL(url, protocol, username, password, m_host, portbuf, path); if(protocol.length() > 0 && stricmp(protocol.str(), "HTTPS") == 0) m_use_ssl = true; else m_use_ssl = false; if(portbuf.length() > 0) m_port = atoi(portbuf.str()); else { if(m_use_ssl) m_port = 443; else m_port = 80; } if(m_use_ssl) { m_ssctx.setown(createSecureSocketContext(ClientSocket)); } } m_ofile = ofile; }
int HttpClient::getUrl(const char* url) { if(!url || !*url || !m_times) return 0; StringBuffer protocol, user, passwd, port, path; m_host.clear(); SplitURL(url, protocol, user, passwd, m_host, port, path); if(port.length() > 0) m_port = atoi(port.str()); else { if(protocol.length() > 0 && stricmp(protocol.str(), "https") == 0) m_port = 443; else m_port = 80; } if(stricmp(protocol.str(), "HTTPS") == 0) m_use_ssl = true; if(m_use_ssl) { #if USE_OPENSSL if(m_ssctx.get() == NULL) m_ssctx.setown(createSecureSocketContext(ClientSocket)); #else throw MakeStringException(-1, "HttpClient: failure to create SSL socket - OpenSSL not enabled in build"); #endif } StringBuffer request; request.appendf("GET %s HTTP/1.0\r\n", path.str()); request.append("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*\r\n"); request.append("Accept-Language: en-us\r\n"); //request.append("Accept-Encoding: gzip, deflate\r\n"); request.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"); request.append("Host: ").append(m_host.str()); if(m_port != 80) request.appendf(":%d", m_port); request.append("\r\n"); if(user.length() > 0) { StringBuffer auth, abuf; abuf.appendf("%s:%s", user.str(), passwd.str()); JBASE64_Encode(abuf.str(), abuf.length(), auth); request.appendf("Authorization: Basic %s\r\n", auth.str()); } request.append("\r\n"); return sendRequest(request); }
HttpProxy::HttpProxy(int localport, const char* host, int port, FILE* ofile, bool use_ssl, IPropertyTree* sslconfig) { m_localport = localport; m_host.append(host); m_port = port; m_ofile = ofile; m_use_ssl = use_ssl; if(use_ssl) { if(sslconfig != NULL) m_ssctx.setown(createSecureSocketContextEx2(sslconfig, ClientSocket)); else m_ssctx.setown(createSecureSocketContext(ClientSocket)); } }
HttpServer::HttpServer(int port, const char* in, FILE* ofile, bool use_ssl, IPropertyTree* sslconfig) { m_ifname.append(in); m_port = port; m_ofile = ofile; m_use_ssl = use_ssl; m_recvDelay = m_sendDelay = m_closeDelay = 0; if(use_ssl) { #ifdef USE_OPENSSL if(sslconfig != NULL) m_ssctx.setown(createSecureSocketContextEx2(sslconfig, ServerSocket)); else m_ssctx.setown(createSecureSocketContext(ServerSocket)); #else throw MakeStringException(-1, "HttpServer: failure to create SSL socket - OpenSSL not enabled in build"); #endif } }
HttpProxy::HttpProxy(int localport, const char* host, int port, FILE* ofile, bool use_ssl, IPropertyTree* sslconfig) { m_localport = localport; m_host.append(host); m_port = port; m_ofile = ofile; m_use_ssl = use_ssl; if(use_ssl) { #if USE_OPENSSL if(sslconfig != NULL) m_ssctx.setown(createSecureSocketContextEx2(sslconfig, ClientSocket)); else m_ssctx.setown(createSecureSocketContext(ClientSocket)); #else throw MakeStringException(-1, "HttpProxy: failure to create SSL connection to host '%s': OpenSSL not enabled in build", host); #endif } }
int HttpClient::sendSoapRequest(const char* url, const char* soapaction, const char* infile) { if(!url || !*url || !infile || !*infile) return 0; StringBuffer protocol, user, passwd, port, path; m_host.clear(); SplitURL(url, protocol, user, passwd, m_host, port, path); if(port.length() > 0) m_port = atoi(port.str()); else { if(protocol.length() > 0 && stricmp(protocol.str(), "https") == 0) m_port = 443; else m_port = 80; } if(stricmp(protocol.str(), "HTTPS") == 0) m_use_ssl = true; if(m_use_ssl) { #ifdef USE_OPENSSL if(m_ssctx.get() == NULL) m_ssctx.setown(createSecureSocketContext(ClientSocket)); #else throw MakeStringException(-1, "HttpClient: failure to create SSL socket - OpenSSL not enabled in build"); #endif } StringBuffer request; try { request.loadFile(infile, true); } catch(IException* e) { StringBuffer errmsg; printf("\nerror loading file %s - %s", infile, e->errorMessage(errmsg).str()); return -1; } catch(...) { printf("\nerror loading file %s", infile); return -1; } if(request.length() == 0) { printf("input is empty\n"); return -1; } const char* ptr = request.str(); while(*ptr == ' ') ptr++; if(*ptr != '<') { printf("the input should be xml\n"); return -1; } if(strncmp(ptr, "<?xml", 5) != 0 && strncmp(ptr, "<soap:Envelope", 14) != 0) { request.insert(0, "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/04/secext\"><soap:Body>"); request.append("</soap:Body></soap:Envelope>"); } StringBuffer headers; headers.appendf("POST %s HTTP/1.1\r\n", path.str()); headers.append("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*\r\n"); headers.append("Accept-Language: en-us\r\n"); headers.append("Content-Type: text/xml\r\n"); if(soapaction && *soapaction) headers.appendf("SOAPAction: \"%s\"\r\n", soapaction); //headers.append("Accept-Encoding: gzip, deflate\r\n"); headers.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"); headers.appendf("Content-Length: %d\r\n", request.length()); headers.append("Host: ").append(m_host.str()); if(m_port != 80) headers.appendf(":%d", m_port); headers.append("\r\n"); if(user.length() > 0) { StringBuffer auth, abuf; abuf.appendf("%s:%s", user.str(), passwd.str()); JBASE64_Encode(abuf.str(), abuf.length(), auth); headers.appendf("Authorization: Basic %s\r\n", auth.str()); } headers.append("\r\n"); request.insert(0, headers.str()); return sendRequest(request); }
int doSendQuery(const char * ip, unsigned port, const char * base) { Owned<ISocket> socket; Owned<ISecureSocketContext> secureContext; __int64 starttime, endtime; StringBuffer ipstr; try { if (strcmp(ip, ".")==0) ip = GetCachedHostName(); else { const char *dash = strchr(ip, '-'); if (dash && isdigit(dash[1]) && dash>ip && isdigit(dash[-1])) { if (persistConnections) UNIMPLEMENTED; const char *startrange = dash-1; while (isdigit(startrange[-1])) startrange--; char *endptr; unsigned firstnum = atoi(startrange); unsigned lastnum = strtol(dash+1, &endptr, 10); if (lastnum > firstnum) { static unsigned counter; static CriticalSection counterCrit; CriticalBlock b(counterCrit); ipstr.append(startrange - ip, ip).append((counter++ % (lastnum+1-firstnum)) + firstnum).append(endptr); ip = ipstr.str(); printf("Sending to %s\n", ip); } } } starttime= get_cycles_now(); if (persistConnections) { if (!persistSocket) { SocketEndpoint ep(ip,port); persistSocket.setown(ISocket::connect_timeout(ep, 1000)); if (useSSL) { #ifdef _USE_OPENSSL if (!persistSecureContext) persistSecureContext.setown(createSecureSocketContext(ClientSocket)); persistSSock.setown(persistSecureContext->createSecureSocket(persistSocket.getClear())); persistSSock->secure_connect(); persistSocket.setown(persistSSock.getClear()); #else throw MakeStringException(-1, "OpenSSL disabled in build"); #endif } } socket = persistSocket; } else { SocketEndpoint ep(ip,port); socket.setown(ISocket::connect_timeout(ep, 100000)); if (useSSL) { #ifdef _USE_OPENSSL secureContext.setown(createSecureSocketContext(ClientSocket)); Owned<ISecureSocket> ssock = secureContext->createSecureSocket(socket.getClear()); ssock->secure_connect(); socket.setown(ssock.getClear()); #else throw MakeStringException(1, "OpenSSL disabled in build"); #endif } } } catch(IException * e) { pexception("failed to connect to server", e); return 1; } StringBuffer fullQuery; bool useHTTP = forceHTTP || strstr(base, "<soap:Envelope") != NULL; if (useHTTP) { StringBuffer newQuery; Owned<IPTree> p = createPTreeFromXMLString(base, ipt_none, ptr_none); const char *queryName = p->queryName(); if ((stricmp(queryName, "envelope") != 0) && (stricmp(queryName, "envelope") != 0)) { if (queryNameOverride.length()) queryName = queryNameOverride; newQuery.appendf("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><Body><%sRequest>", queryName); Owned<IPTreeIterator> elements = p->getElements("./*"); ForEach(*elements) { IPTree &elem = elements->query(); toXML(&elem, newQuery, 0, XML_SingleQuoteAttributeValues); } newQuery.appendf("</%sRequest></Body></Envelope>", queryName); base = newQuery.str(); } // note - don't support queryname override unless original query is xml fullQuery.appendf("POST /doc HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n", (int) strlen(base)).append(base); } else { if (sendToSocket)