bool exportToTarget(const OS_NAMESPACE_NAME::XMLDocument &document, XMLFormatTarget *target, const OS_NAMESPACE_NAME::String &encoding) { try { AutoPtr<DOMDocument> doc = getImplementation()->createDocument(); if(convertDocument(document, doc) == false) return false; AutoPtr<DOMLSSerializer> writer(createWriter()); AutoPtr<DOMLSOutput> stream(createOutputStream(encoding)); stream->setByteStream(target); writer->write(doc, stream); return true; } catch(const XMLException &e) { OS_LOG_ERROR(e.getMessage()); } catch(const DOMException &e) { OS_LOG_ERROR(e.getMessage()); } catch(...) { OS_LOG_ERROR(OS_ERR_UNKNOWN(xml)); } return false; }
void HttpServer::handleOneRequest(ISocket* client) { char peername[256]; int port = client->peer_name(peername, 256); if(httptest_tracelevel > 5) fprintf(m_ofile, "\n>>receivd request from %s:%d\n", peername, port); StringBuffer requestbuf; Owned<IByteOutputStream> reqstream = createOutputStream(requestbuf); if (m_recvDelay>0) sleep(m_recvDelay); receiveData(client, reqstream.get(), false); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s", sepstr, requestbuf.str(), sepstr); if(m_response.length() == 0) { //const char* resp_body = "<html><head><meta http-equiv=\"refresh\" content=\"3; url=http://ymaxp:8020\"/></head><body>Default response from httptest server mode</body></html>"; //const char* resp_body = "<html><body onLoad=window.setTimeout(\"location.href='http://ymaxp:8020'\",10000)>Default response from httptest server mode</body></html>"; const char* resp_body = "<html><head><title>default response</title></head><body>Default response from httptest server mode</body></html>"; //const char* resp_body = "<html><head><title>default response</title></head><body><IFRAME SRC=\"http://www.yahoo.com\" TITLE=\"esp config xml file\" width=\"100%\" height=\"100%\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\"></IFrame></body></html>"; m_response.append("HTTP/1.1 200 OK\r\n"); m_response.append("Content-Type: text/html; charset=UTF-8\r\n"); m_response.appendf("Content-Length: %d\r\n", (int) strlen(resp_body)); m_response.appendf("Subject: my-title\r\n"); m_response.append("Expires: 0\r\n"); m_response.append("\r\n"); m_response.append(resp_body); } if (m_sendDelay) sleep(m_sendDelay); client->write(m_response.str(), m_response.length()); if(httptest_tracelevel > 10) fprintf(m_ofile, "\n>>sent back response - \n"); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s\n", sepstr, m_response.str(), sepstr); fflush(m_ofile); if (m_closeDelay) sleep(m_closeDelay); client->close(); }
int CHttpProxyThread::run() { Thread::Link(); int ret = 0; try { char peername[256]; int clientport = m_client->peer_name(peername, 256); if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>receivd request from %s:%d\n", peername, clientport); char oneline[2049]; memset(oneline, 0, 2049); bool socketclosed = false; int lenread = readline(m_client.get(), oneline, 2048, socketclosed); if(http_tracelevel >= 10) printf("firstline=%s\n", oneline); if(strncmp(oneline, "CONNECT ", 8) == 0) { char* curptr = oneline + 8; while(*curptr && *curptr == ' ') curptr++; const char* hostptr = curptr; while(*curptr && *curptr != ':' && *curptr != ' ') curptr++; int port = 80; if(*curptr == ':') { *curptr = 0; curptr++; const char* portptr = curptr; while(*curptr && *curptr != ' ') curptr++; *curptr = 0; if(*portptr) port = atoi(portptr); } StringBuffer host(hostptr); while(lenread > 2 && !socketclosed) lenread = readline(m_client.get(), oneline, 2048, socketclosed); SocketEndpoint ep; ep.set(host.str(), port); m_remotesocket.setown(ISocket::connect(ep)); const char* resp = "HTTP/1.0 200 Connection established\r\n" "Proxy-agent: Netscape-Proxy/1.1\r\n\r\n"; m_client->write(resp, strlen(resp)); m_client->set_nonblock(false); m_remotesocket->set_nonblock(false); CReadWriteThread t1(m_client.get(), m_remotesocket.get(), m_ofile); CReadWriteThread t2(m_remotesocket.get(), m_client.get(), m_ofile); t1.start(); t2.start(); t1.join(); t2.join(); //printf("read/write threads returned\n"); m_remotesocket->shutdown(); m_remotesocket->close(); m_client->shutdown(); m_client->close(); } else { const char* http = strstr(oneline, "http://"); if(!http) http = strstr(oneline, "HTTP://"); if(!http) throw MakeStringException(-1, "protocol not recognized\n"); StringBuffer requestbuf; requestbuf.append(http - oneline, oneline); const char* slash = http + 7; while(*slash && *slash != '/' && *slash != ' ' && *slash != '\r') slash++; if(*slash != '/') requestbuf.append('/'); else requestbuf.append(slash); Owned<IByteOutputStream> reqstream = createOutputStream(requestbuf); bool isRoxie; Http::receiveData(m_client, reqstream.get(), false, isRoxie, "Proxy-Connection", NULL, NULL, true); if(http_tracelevel >= 5) fprintf(m_ofile, "Received request from %s\n", peername); if(http_tracelevel >= 10) fprintf(m_ofile, "%s\n", requestbuf.str()); const char* hostname = http + 7; char* ptr = (char*)hostname; while(*ptr && *ptr != ':' && *ptr != '/' && *ptr != ' ') ptr++; int port = 80; if(*ptr == ':') { *ptr = 0; ptr++; const char* portptr = ptr; while(*ptr && *ptr != ' ' && *ptr != '/') ptr++; if(*ptr) *ptr = 0; if(portptr) port = atoi(portptr); } else *ptr = 0; SocketEndpoint ep; ep.set(hostname, port); m_remotesocket.setown(ISocket::connect(ep)); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sending request to %s:%d\n", hostname, port); m_remotesocket->write(requestbuf.str(), requestbuf.length()); StringBuffer respbuf; Owned<CSocketOutputStream> respstream = new CSocketOutputStream(m_client.get()); Http::receiveData(m_remotesocket.get(), respstream.get(), true, isRoxie); if(http_tracelevel >= 5) fprintf(m_ofile, ">>receivd response from %s:%d:\n", hostname, port); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sent response back to %s:%d\n", peername, clientport); fflush(m_ofile); m_remotesocket->shutdown(); m_remotesocket->close(); m_client->shutdown(); m_client->close(); } } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("%s", excpt->errorMessage(errMsg).str()); ret = -1; } catch(...) { DBGLOG("unknown exception"); ret = -1; } Thread::Release(); return 0; }
int COneServerHttpProxyThread::start() { try { char peername[256]; int port = m_client->peer_name(peername, 256); if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>receivd request from %s:%d\n", peername, port); StringBuffer requestbuf; Owned<IByteOutputStream> reqstream = createOutputStream(requestbuf); bool isRoxie; Http::receiveData(m_client, reqstream.get(), false, isRoxie); if(http_tracelevel >= 10) fprintf(m_ofile, "%s%s%s", sepstr, requestbuf.str(), sepstr); else if(http_tracelevel >= 5) { const char* endofline = strstr(requestbuf.str(), "\n"); if(endofline) { StringBuffer firstline; firstline.append((endofline - requestbuf.str()), requestbuf.str()); fprintf(m_ofile, "%s", firstline.str()); } else fprintf(m_ofile, "%s\n", requestbuf.str()); } if (0 != stricmp(m_url_prefix, "/")) { int url_offset; if (!strnicmp(requestbuf.str(), "GET ", 4)) url_offset = 4; else if (!strnicmp(requestbuf.str(), "POST ", 5)) url_offset = 5; else url_offset = -1; if (url_offset > 0) { const int prefix_len = strlen(m_url_prefix); if (0 != strnicmp(requestbuf.str()+url_offset, m_url_prefix, prefix_len)) { const char* endofline = strstr(requestbuf.str(), "\n"); if(endofline) { StringBuffer firstline; firstline.append((endofline - requestbuf.str()), requestbuf.str()); fprintf(m_ofile, "INVALID request: %s", firstline.str()); } else fprintf(m_ofile, "INVALID request:\n%s\n", requestbuf.str()); StringBuffer respbuf; respbuf.append("HTTP/1.1 404 Not Found\n") .append("Content-Type: text/xml; charset=UTF-8\n") .append("Connection: close\n"); m_client->write(respbuf.str(), respbuf.length()); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sent the response back to %s:%d:\n", peername, port); if(http_tracelevel >= 10) fprintf(m_ofile, "%s%s%s", sepstr, respbuf.str(), sepstr); fflush(m_ofile); m_client->shutdown(); m_client->close(); return -1; } else { //we want to map /x to / and /x/y to /y as follows: //if m_url_prefix is /x and url is /x/y then remove x //to result in //y requestbuf.remove(++url_offset, prefix_len-1); //now, if we have //y then change it to /y if (*(requestbuf.str()+url_offset) == '/') requestbuf.remove(url_offset, 1); } } } SocketEndpoint ep; Owned<ISocket> socket2; ep.set(m_host.str(), m_port); socket2.setown(ISocket::connect(ep)); if(m_use_ssl && m_ssctx != NULL) { Owned<ISecureSocket> securesocket = m_ssctx->createSecureSocket(socket2.getLink()); int res = securesocket->secure_connect(); if(res >= 0) { socket2.set(securesocket.get()); } } if(socket2.get() == NULL) { StringBuffer urlstr; DBGLOG(">>Can't connect to %s", ep.getUrlStr(urlstr).str()); return -1; } char newhost[1024]; sprintf(newhost, "%s:%d", m_host.str(), m_port); replaceHeader(requestbuf, "Host", newhost); //checkContentLength(requestbuf); if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>sending request to %s:%d\n", m_host.str(), m_port); if(http_tracelevel >= 10) fprintf(m_ofile, "%s%s%s", sepstr, requestbuf.str(), sepstr); socket2->write(requestbuf.str(), requestbuf.length()); StringBuffer respbuf; Owned<IByteOutputStream> respstream = createOutputStream(respbuf); isRoxie; Http::receiveData(socket2.get(), respstream.get(), true, isRoxie); if(http_tracelevel >= 5) fprintf(m_ofile, ">>received response from %s:%d:\n", m_host.str(), m_port); if(http_tracelevel >= 10) fprintf(m_ofile, "%s%s%s", sepstr, respbuf.str(), sepstr); m_client->write(respbuf.str(), respbuf.length()); fflush(m_ofile); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sent the response back to %s:%d:\n", peername, port); if(http_tracelevel >= 10) fprintf(m_ofile, "%s%s%s", sepstr, respbuf.str(), sepstr); socket2->shutdown(); socket2->close(); m_client->shutdown(); m_client->close(); } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("%s", excpt->errorMessage(errMsg).str()); return -1; } catch(...) { DBGLOG("unknown exception"); return -1; } return 0; }
int COneServerHttpProxyThread::start() { try { char peername[256]; int port = m_client->peer_name(peername, 256); if(httptest_tracelevel > 5) fprintf(m_ofile, "\n>>receivd request from %s:%d\n", peername, port); StringBuffer requestbuf; Owned<IByteOutputStream> reqstream = createOutputStream(requestbuf); receiveData(m_client, reqstream.get(), false); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s", sepstr, requestbuf.str(), sepstr); SocketEndpoint ep; Owned<ISocket> socket2; ep.set(m_host.str(), m_port); socket2.setown(ISocket::connect(ep)); if(m_use_ssl && m_ssctx != NULL) { Owned<ISecureSocket> securesocket = m_ssctx->createSecureSocket(socket2.getLink()); int res = securesocket->secure_connect(); if(res >= 0) { socket2.set(securesocket.get()); } } if(socket2.get() == NULL) { StringBuffer urlstr; DBGLOG(">>Can't connect to %s", ep.getUrlStr(urlstr).str()); return -1; } char newhost[1024]; sprintf(newhost, "%s:%d", m_host.str(), m_port); replaceHeader(requestbuf, "Host", newhost); //checkContentLength(requestbuf); if(httptest_tracelevel > 5) fprintf(m_ofile, "\n>>sending request to %s:%d\n", m_host.str(), m_port); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s", sepstr, requestbuf.str(), sepstr); socket2->write(requestbuf.str(), requestbuf.length()); StringBuffer respbuf; Owned<IByteOutputStream> respstream = createOutputStream(respbuf); receiveData(socket2.get(), respstream.get(), true); if(httptest_tracelevel > 5) fprintf(m_ofile, ">>received response from %s:%d:\n", m_host.str(), m_port); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s", sepstr, respbuf.str(), sepstr); m_client->write(respbuf.str(), respbuf.length()); fflush(m_ofile); if(httptest_tracelevel > 5) fprintf(m_ofile, ">>sent the response back to %s:%d:\n", peername, port); socket2->shutdown(); socket2->close(); m_client->shutdown(); m_client->close(); } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("%s", excpt->errorMessage(errMsg).str()); return -1; } catch(...) { DBGLOG("unknown exception"); return -1; } return 0; }
int HttpClient::sendRequest(int times, HttpStat& stat, StringBuffer& req) { StringBuffer request; if(req.length() <= 2) { throw MakeStringException(-1, "request too short"); } bool endofheaders = false; char c0 = req.charAt(0); char c1 = req.charAt(1); if(c0 == '\n') request.append("\r\n"); else request.append(c0); if(c1 == '\n') { if(c0 == '\r') request.append(c1); else { request.append("\r\n"); if(c0 == '\n') endofheaders = true; } } else request.append(c1); unsigned seq = 2; while(seq < req.length() && !endofheaders) { char c = req.charAt(seq); if(c == '\n') { char c1 = req.charAt(seq - 1); char c2 = req.charAt(seq - 2); if(c1 == '\n' || (c1 == '\r' && c2 == '\n')) endofheaders = true; if(c1 != '\r') request.append("\r\n"); else request.append(c); } else request.append(c); seq++; } if(seq < req.length()) request.append(req.length() - seq, req.str() + seq); if(httptest_tracelevel > 5) fprintf(m_ofile, ">>sending out request to %s:%d for %d times\n", m_host.str(), m_port, times); unsigned start = msTick(); int slowest = 0; int fastest = 2147483647; for(int i = 0; i < times; i++) { SocketEndpoint ep; ep.set(m_host.str(), m_port); Owned<ISocket> socket; try { socket.setown(ISocket::connect(ep)); if(m_use_ssl && m_ssctx.get() != NULL) { Owned<ISecureSocket> securesocket = m_ssctx->createSecureSocket(socket.getLink()); int res = securesocket->secure_connect(); if(res >= 0) { socket.set(securesocket.get()); } } } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("Error connecting to %s:%d - %d:%s", m_host.str(), m_port, excpt->errorCode(), excpt->errorMessage(errMsg).str()); continue; } catch(...) { DBGLOG("can't connect to %s:%d", m_host.str(), m_port); continue; } if(socket.get() == NULL) { StringBuffer urlstr; DBGLOG(">>Can't connect to %s", ep.getUrlStr(urlstr).str()); continue; } if(m_delay > 0) sleep(m_delay); if(httptest_tracelevel > 5) fprintf(m_ofile, ">>sending out request:\n"); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s\n", sepstr, request.str(), sepstr); unsigned start1 = msTick(); socket->write(request.str(), request.length()); if(httptest_tracelevel > 5) fprintf(m_ofile, ">>receiving response:\n"); StringBuffer buf; Owned<IByteOutputStream> ostream = createOutputStream(buf); stat.totalresplen += receiveData(socket.get(), ostream.get(), true); if(httptest_tracelevel > 10) fprintf(m_ofile, "%s%s%s\n", sepstr, buf.str(), sepstr); char tmpbuf[256]; unsigned int sizeread; do { socket->read(tmpbuf, 0, 256, sizeread); } while(sizeread > 0); socket->shutdown(); socket->close(); fflush(m_ofile); unsigned end1 = msTick(); int duration = end1 - start1; if(duration <= fastest) fastest = duration; if(duration > slowest) slowest = duration; if(i % 100 == 0) fprintf(stderr, "sent out %d\n", i); } unsigned end = msTick(); stat.msecs = end - start; stat.numrequests = times; stat.totalreqlen = times * request.length(); stat.slowest = slowest; stat.fastest = fastest; return 0; }