本文整理汇总了C++中HTTPServerResponse类的典型用法代码示例。如果您正苦于以下问题:C++ HTTPServerResponse类的具体用法?C++ HTTPServerResponse怎么用?C++ HTTPServerResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HTTPServerResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType(request.getContentType());
std::ostream& ostr = response.send();
Poco::StreamCopier::copyStream(request.stream(), ostr);
}
示例2: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
app.logger().information("Request from " + request.clientAddress().toString());
SecureStreamSocket socket = static_cast<HTTPServerRequestImpl&>(request).socket();
if (socket.havePeerCertificate())
{
X509Certificate cert = socket.peerCertificate();
app.logger().information("Client certificate: " + cert.subjectName());
}
else
{
app.logger().information("No client certificate available.");
}
Timestamp now;
std::string dt(DateTimeFormatter::format(now, _format));
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html><head><title>HTTPTimeServer powered by POCO C++ Libraries</title>";
ostr << "<meta http-equiv=\"refresh\" content=\"1\"></head>";
ostr << "<body><p style=\"text-align: center; font-size: 48px;\">";
ostr << dt;
ostr << "</p></body></html>";
}
示例3:
void PageRequestHandler::handleRequest
(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
const char *message = "The quick brown fox jumps over the lazy dog.\n";
for(int i {0}; i < 100; ++i)
{
response.sendBuffer(message, 44);
}
}
示例4: handleRequest
void PageRequestHandler::handleRequest( HTTPServerRequest& request,
HTTPServerResponse& response )
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr<<"<html>";
ostr<<"<body>";
ostr<<"Stub (HTML) output - insert JavaScript HERE";
ostr<<"</body>";
ostr<<"</html>";
}
示例5: handleRequest
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp){
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("text/html");
// Generate requested HTML file
ostream& out = resp.send();
out << "<h1>Hello world! This files name is " << req.getURI() << "</h1>"
<< "<p>Host: " << req.getHost() << "</p>"
<< "<p>Method: " << req.getMethod() << "</p>";
out.flush();
cout << endl << "Client requested: =" << req.getURI() << endl;
}
示例6: isValidRequest
//------------------------------------------------------------------------------
bool ofxWebServerBaseRouteHandler::isValidRequest(const Settings& settings,
HTTPServerRequest& request,
HTTPServerResponse& response) {
string sessionId = "";
// extract cookie from request
NameValueCollection cookies;
request.getCookies(cookies);
NameValueCollection::ConstIterator it = cookies.find(settings.sessionCookieName);
if (it != cookies.end()) {
sessionId = it->second;
} else {
sessionId = ofxWebServerSessionManager::generateSessionKey(request);
HTTPCookie cookie(settings.sessionCookieName,sessionId);
cookie.setPath("/");
// set no age, so it expires @ end of session
response.addCookie(cookie);
}
// TODO: update session manager
URI uri(request.getURI());
const string path = uri.getPath(); // just get the path
if(settings.requireAuthentication) {
if(request.hasCredentials()) {
HTTPBasicCredentials credentials(request);
const string& user = credentials.getUsername();
const string& pwd = credentials.getPassword();
if(settings.username == credentials.getUsername() &&
settings.password == credentials.getPassword()) {
// add an authentication cookie?
return true;
} else {
response.setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED);
sendErrorResponse(response);
return false;
}
} else {
response.requireAuthentication(settings.realm);
response.setContentLength(0);
response.send();
return false;
}
} else {
return true;
}
}
示例7: handleRequest
//------------------------------------------------------------------------------
void ofxIpVideoServerRouteHandler::handleRequest(HTTPServerRequest& request,
HTTPServerResponse& response) {
if(isValidRequest(settings.route,request,response)) {
MediaType mediaType("multipart/x-mixed-replace");
mediaType.setParameter("boundary", settings.boundary);
string expires = DateTimeFormatter::format(Timestamp(0), // the beginning of time
DateTimeFormat::HTTP_FORMAT);
response.set("Cache-control","no-cache");
response.setContentType(mediaType);
response.set("Expires",expires);
response.set("Pragma","no-cache");
std::ostream& ostr = response.send(); // get output stream
bool stopStreaming = false;
while(ostr.good() && !stopStreaming) {
if(!queue.empty()) {
ofxIpVideoServerFramePtr frame = queue.pop();
if(frame != NULL) {
ofBuffer* buffer = &frame.get()->buffer;
ostr << settings.boundary;
ostr << "\r\n";
ostr << "Content-Type: image/jpeg";
ostr << "\r\n";
ostr << "Content-Length: " << ofToString(buffer->size());
ostr << "\r\n";
ostr << "\r\n";
ostr << *buffer;
} else {
ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Null buffer.";
}
} else {
ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Queue empty.";
}
Thread::sleep(50);
}
queue.setActive(false); // a desperate move
ofLogNotice("ofxIpVideoServerRouteHandler::handleRequest") << "Client disconneted.";
//sendErrorResponse(response);
}
}
示例8: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Timestamp now;
std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html><head><title>TimeServer powered by POCO ApacheConnector</title>";
ostr << "<meta http-equiv=\"refresh\" content=\"1\"></head>";
ostr << "<body><p style=\"text-align: center; font-size: 48px;\">";
ostr << dt;
ostr << "</p></body></html>";
}
示例9: handleRequest
//------------------------------------------------------------------------------
void ofxWebServerUploadRouteHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) {
if(isValidRequest(settings.route, request, response)) {
HTMLForm form(request, request.stream(), *this);
if(!settings.uploadRedirect.empty()) {
response.redirect(settings.uploadRedirect);
} else {
response.setStatusAndReason(HTTPResponse::HTTP_OK);
response.setContentLength(0);
response.send();
}
} else {
return; // isValidRequest took care of the response
}
}
示例10: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
WebSocket ws(request, response);
XDUTxtSetText(ID_CURRENT_STATUS, Convert::toQXStringRef(L"Выполнено подключение к плагину. Чтобы начать интерпретацию - кликните по соотвествующей кнопке плагина Вашего браузера."));
char buffer[MAX_BUFFER_SIZE];
int flags=0;
int n=1;
do
{
ZeroMemory(buffer, MAX_BUFFER_SIZE);
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
wstring message = Convert::toUnicodeString(buffer);
if (message == L"plugin-version") {
//Следующее сообщение передаст данные о плагине
n = ws.receiveBytes(buffer, sizeof(buffer), flags);
XDUTxtSetText(ID_BROWSER_PLUGIN_VERSION, Convert::toQXStringRef(Convert::toUnicodeString(buffer)));
}
if (message == L"exportSettings") {
n = ws.receiveBytes(buffer, sizeof(buffer), flags);
ImportSettings::globalSettings=Convert::jsonToSettings(Convert::toUnicodeString(buffer));
}
if (message == L"start-export") {
WebCore::getInstance().setSocket(&ws);
WebCore::startExport = true;
while (WebCore::startExport == true);
}
} while (n > 0);
}
catch (WebSocketException& exc)
{
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
示例11: sendErrorResponse
void ofxWebServerBaseRouteHandler::sendErrorResponse(HTTPServerResponse& response) {
// we will assume that the sender has set the status and
// reason appropriately before calling the sendErrorResponse()
HTTPResponse::HTTPStatus status = response.getStatus();
string reason = response.getReason();
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send(); // get output stream
ostr << "<html>";
ostr << "<head><title>" << status << "-" << reason << "</title></head>";
ostr << "<body>";
ostr << "<h1>" << status << "-" << reason << "</h1>";
ostr << "</body>";
ostr << "<html>";
}
示例12: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
WebSocket ws(request, response);
app.logger().information("WebSocket connection established.");
char buffer[BUFF_LEN];
int flags;
int n;
do
{
int recvLen = 0;
while (recvLen < BUFF_LEN) {
n = ws.receiveFrame(buffer, BUFF_LEN - recvLen, flags);
recvLen += n;
}
app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags)));
String resp ("aaaaaaaaaa");
ws.sendFrame(resp.getCStr(), resp.getLength(), flags);
}
while (n > 0 || (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
app.logger().information("WebSocket connection closed.");
}
catch (WebSocketException& exc)
{
app.logger().log(exc);
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
示例13: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
app.logger().information("Request from " + request.clientAddress().toString());
Timestamp now;
std::string dt(DateTimeFormatter::format(now, _format));
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html><head><title>HTTPTimeServer powered by POCO C++ Libraries</title>";
ostr << "<meta http-equiv=\"refresh\" content=\"1\"></head>";
ostr << "<body><p style=\"text-align: center; font-size: 48px;\">";
ostr << dt;
ostr << "</p></body></html>";
}
示例14: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html>";
ostr << "<head>";
ostr << "<title>WebSocketServer</title>";
ostr << "<script type=\"text/javascript\">";
ostr << "function WebSocketTest()";
ostr << "{";
ostr << " if (\"WebSocket\" in window)";
ostr << " {";
ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");";
ostr << " ws.onopen = function()";
ostr << " {";
ostr << " ws.send(\"Hello, world!\");";
ostr << " };";
ostr << " ws.onmessage = function(evt)";
ostr << " { ";
ostr << " var msg = evt.data;";
ostr << " alert(\"Message received: \" + msg);";
ostr << " ws.close();";
ostr << " };";
ostr << " ws.onclose = function()";
ostr << " { ";
ostr << " alert(\"WebSocket closed.\");";
ostr << " };";
ostr << " }";
ostr << " else";
ostr << " {";
ostr << " alert(\"This browser does not support WebSockets.\");";
ostr << " }";
ostr << "}";
ostr << "</script>";
ostr << "</head>";
ostr << "<body>";
ostr << " <h1>WebSocket Server</h1>";
ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>";
ostr << "</body>";
ostr << "</html>";
}
示例15: handleRequest
void handleRequest(HTTPServerRequest& request,
HTTPServerResponse& response)
{
StringTokenizer tok("/");
StringVector tized;
tok.tokenize(request.getURI(), tized);
if ( tized.size() == 4 )
{
int z = as<int>(tized[1], 0);
int x = as<int>(tized[2], 0);
unsigned int y = as<int>(osgDB::getNameLessExtension(tized[3]),0);
std::string ext = osgDB::getFileExtension(tized[3]);
OE_DEBUG << "z=" << z << std::endl;
OE_DEBUG << "x=" << x << std::endl;
OE_DEBUG << "y=" << y << std::endl;
OE_DEBUG << "ext=" << ext << std::endl;
response.setChunkedTransferEncoding(true);
osg::ref_ptr< osg::Image > image = _server->getTile(z, x, y);
if (image)
{
osgDB::ReaderWriter* rw = osgDB::Registry::instance()->getReaderWriterForExtension(ext);
if (rw)
{
std::string mime = "image/png";
if (ext == "jpeg" || ext == "jpg")
{
mime = "image/jpeg";
}
response.setContentType(mime);
std::ostream& ostr = response.send();
rw->writeImage(*image.get(), ostr);
}
}
}
response.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
}