本文整理汇总了C++中HTTPServerResponse::setContentLength方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPServerResponse::setContentLength方法的具体用法?C++ HTTPServerResponse::setContentLength怎么用?C++ HTTPServerResponse::setContentLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPServerResponse
的用法示例。
在下文中一共展示了HTTPServerResponse::setContentLength方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
std::string proto = request.get("Sec-WebSocket-Protocol", "");
Poco::SharedPtr<Poco::Net::WebSocket> pWebSocket;
if (proto == "com.appinf.webtunnel.server/1.0")
{
response.set("Sec-WebSocket-Protocol", proto);
pWebSocket = new Poco::Net::WebSocket(request, response);
_portReflector.addServerSocket(pWebSocket, "ac9667bb-6032-4267-af61-9a7aafd40479");
}
else if (proto == "com.appinf.webtunnel.client/1.0")
{
response.set("Sec-WebSocket-Protocol", proto);
std::string portStr = request.get("X-WebTunnel-RemotePort", "");
unsigned port;
if (!portStr.empty() && Poco::NumberParser::tryParseUnsigned(portStr, port) && port > 0 && port < 65536)
{
pWebSocket = new Poco::Net::WebSocket(request, response);
try
{
_portReflector.addClientSocket(pWebSocket, "ac9667bb-6032-4267-af61-9a7aafd40479", static_cast<Poco::UInt16>(port));
}
catch (Poco::NotFoundException&)
{
pWebSocket->shutdown(Poco::Net::WebSocket::WS_UNEXPECTED_CONDITION, "No connection to target available");
}
}
else
{
pWebSocket = new Poco::Net::WebSocket(request, response);
pWebSocket->shutdown(Poco::Net::WebSocket::WS_UNEXPECTED_CONDITION, "Missing or invalid X-WebTunnel-RemotePort header");
}
}
else
{
pWebSocket = new Poco::Net::WebSocket(request, response);
pWebSocket->shutdown(Poco::Net::WebSocket::WS_PROTOCOL_ERROR);
}
}
catch (WebSocketException& exc)
{
app.logger().log(exc);
switch (exc.code())
{
case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case Poco::Net::WebSocket::WS_ERR_NO_HANDSHAKE:
case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
示例2: 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;
}
}
示例3: 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
}
}
示例4: 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;
}
}
}
示例5: 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;
}
}
}
示例6: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
/// Creates WebSocket and accepts the connection request from web client.
{
try
{
if (!_pWS)
{
_pWS = new WebSocket(request, response);
Timespan ts(600, 0);
_pWS->setReceiveTimeout(ts);
_pWS->setSendTimeout(ts);
}
std::cout << std::endl << "WebSocket connection established." << std::endl << PROMPT;
char buffer[1024];
int n, count = 0;
do
{
n = _pWS->receiveFrame(buffer, sizeof(buffer), _flags);
}
while (n > 0 || (_flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
std::cout << "WebSocket connection closed." << std::endl;
}
catch (WebSocketException& exc)
{
std::cout << exc.displayText() << std::endl;
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;
}
}
}
示例7: handleRequest
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
WebSocket ws(request, response);
printf("getReceiveTimeout %d\n", ws.getReceiveTimeout().totalSeconds());
app.logger().information("WebSocket connection established.");
char buffer[1024];
int flags;
int n;
do
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags)));
ws.sendFrame(buffer, n, 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;
}
}
app.logger().information("WebSocket connection terminated.");
}
示例8: handleRequest
void client_request_handler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
try
{
LOG("Request for client connection");
// any exceptions thrown on WebSocket handshake or client validation
// will lead to not registering client
ws_t ws(new WebSocket(request, response));
client_t h(new client(ws));
h->init();
CLIENTS.add(h);
}
catch (const WebSocketException& exc)
{
LOGERROR(exc.displayText());
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;
}
}
catch (const Exception& e)
{
LOGERROR(e.displayText());
}
catch (const exception& e)
{
LOGERROR(e.what());
}
}