本文整理汇总了C++中poco::net::HTTPServerResponse::setContentType方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPServerResponse::setContentType方法的具体用法?C++ HTTPServerResponse::setContentType怎么用?C++ HTTPServerResponse::setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类poco::net::HTTPServerResponse
的用法示例。
在下文中一共展示了HTTPServerResponse::setContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleRequest
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
{
if(request.getURI()=="/command") {
EachInputValue(request, [&](const char *id, const char *command){
wdmEventData tmp = {std::atoi(id), command};
wdmSystem::getInstance()->addEvent(tmp);
});
response.setContentType("text/plain");
response.setContentLength(2);
std::ostream &ostr = response.send();
ostr.write("ok", 3);
}
else if(request.getURI()=="/data") {
std::vector<wdmID> nodes;
nodes.push_back(_wdmGetRootNode()->getID());
EachNodeValue(request, [&](const char *id){
nodes.push_back(std::atoi(id));
});
wdmString json;
wdmJSONRequest request = {false, false, &json, nodes.empty() ? NULL : &nodes[0], (uint32_t)nodes.size()};
wdmSystem::getInstance()->requestJSON(request);
while(!request.done) { Poco::Thread::sleep(2); }
if(request.canceled) { json="[]"; }
response.setContentType("application/json");
response.setContentLength(json.size());
std::ostream &ostr = response.send();
ostr.write(&json[0], json.size());
}
}
示例2: setContentType
void FileRequestHandler::setContentType(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
Poco::Path path(request.getURI());
std::string extension = Poco::toLower(path.getExtension());
if (extension == "css") {
response.setContentType("text/css");
}
else if (extension == "js") {
response.setContentType("application/javascript");
}
else {
response.setContentType("text/html");
}
}
示例3: handleRequest
void RESTHandler::handleRequest(Poco::Net::HTTPServerRequest &request,
Poco::Net::HTTPServerResponse &response) {
if (verbose) {
std::clog << "HTTP request " << request.getURI() << std::endl;
std::clog << "Context id: " << client.context_id() << std::endl;
}
zmqpp::message msg, reply;
/// Connect to broker if not connected
client.connect(broker);
Poco::URI url(request.getURI());
Poco::Net::HTMLForm form(request);
/// Filter by black list
if (black_list.find(url.getPath()) != black_list.end()) {
return error_black_list(response);
}
if (!build_message(request, form, url, msg)) {
return error_parse(response);
}
if (!client.send_request(msg, reply, (form.has("timeout") ? std::stoi(form.get("timeout")) : timeout))) {
return error_timeout(response);
}
/// Render response
response.setStatus(Poco::Net::HTTPServerResponse::HTTPStatus::HTTP_OK);
if (form.get("type", "json") == "json") {
/// JSON in single line (FastWriter)
std::string jsonp_callback = form.get("jsonp", form.get("callback", ""));
Json::Value packet(Json::ValueType::arrayValue);
response.setContentType("application/json");
std::ostream &out = response.send();
if (!jsonp_callback.empty())
out << jsonp_callback << "(";
for (size_t part = 0; part < reply.parts(); ++part)
packet.append(reply.get(part));
auto txt = writer.write(packet);
if (txt[txt.size() - 1] == '\n') // Cheat for EOL in serialization
txt = txt.substr(0, txt.size() - 1);
out << txt << (!jsonp_callback.empty() ? ")" : "") << std::flush;
} else {
/// Plain text wihtout delimiters
response.setContentType("text/plain");
std::ostream &out = response.send();
for (size_t part = 0; part < reply.parts(); ++part)
out.write((char *) reply.raw_data(part), reply.size(part));
out.flush();
}
}
示例4: handleRequest
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
response.setContentType("text/xml");
std::ostream& ostr = response.send();
redis r;
r.arg("zrange");
r.arg("history");
r.arg("0");
r.arg("-1");
std::list<std::string> result;
r.exec_array(result);
std::string rss;
rss =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<rss version=\"2.0\">"
" <channel>"
" <title>rss</title>"
" <link>http://localhost/</link>"
" <description>merge rss feeds</description>";
for (std::list<std::string>::reverse_iterator it = result.rbegin(), end = result.rend(); it != end; ++it)
{
r.arg("hget");
r.arg("items");
r.arg(*it);
rss += r.exec_string().second;
}
rss += "</channel></rss>";
ostr << rss << std::flush;
}
示例5: handleRequest
void GenericFileHandler::handleRequest(Poco::Net::HTTPServerRequest & req, Poco::Net::HTTPServerResponse & resp) {
std::ifstream file;
try {
file.open(fileName.c_str(), std::ifstream::in);
} catch (...) {
}
if (!file.is_open()) {
resp.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
resp.send();
return;
}
std::string wsdl;
while (!file.eof()) {
std::string tmp;
std::getline(file, tmp);
wsdl += tmp;
}
file.close();
resp.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
resp.setContentType("application/xml");
resp.setChunkedTransferEncoding(false);
resp.setContentLength(wsdl.length());
std::ostream & out = resp.send();
out << wsdl << std::flush;
}
示例6: handleRequest
void TimeHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
Poco::Net::HTMLForm form(request, request.stream());
std::ostream& responseStream = response.send();
responseStream << "";
responseStream << "\n";
responseStream << "";
responseStream << "\n";
responseStream << "\n";
responseStream << "";
#line 6 "/ws/poco-1.3/PageCompiler/samples/HTTPTimeServer/src/TimeHandler.cpsp"
Poco::DateTime now;
std::string dt(Poco::DateTimeFormatter::format(now, "%W, %e %b %y %H:%M:%S %Z"));
responseStream << "\n";
responseStream << "<html>\n";
responseStream << "<head>\n";
responseStream << "<title>HTTPTimeServer powered by POCO C++ Libraries and PageCompiler</title>\n";
responseStream << "<meta http-equiv=\"refresh\" content=\"1\">\n";
responseStream << "</head>\n";
responseStream << "<body>\n";
responseStream << "<p style=\"text-align: center; font-size: 48px;\">";
#line 16 "/ws/poco-1.3/PageCompiler/samples/HTTPTimeServer/src/TimeHandler.cpsp"
responseStream << ( dt );
responseStream << "</p>\n";
responseStream << "</body>\n";
responseStream << "</html>\n";
responseStream << "";
}
示例7: streamFile
static void streamFile(Poco::Net::HTTPServerResponse & response, std::string file, std::string mime) {
response.setContentType(mime);
response.setChunkedTransferEncoding(true);
std::ostream & o = response.send();
std::ifstream docf(file.c_str());
Poco::StreamCopier::copyStream(docf, o);
}
示例8: handleRequest
void FixedPathHandler::handleRequest(Poco::Net::HTTPServerRequest &, Poco::Net::HTTPServerResponse &response) {
Poco::Net::MediaType mediaType("text", "plain");
response.setContentType(mediaType);
response.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
std::ostream &stream = response.send();
stream << "value";
}
示例9: handleRequest
virtual void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
{
if (request.getURI() == "/name")
{
response.setContentType("application/json");
response.send() << " { \"hanwenfang\"} ";
}
}
示例10: handleRequest
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) {
jsonrpc::handleRequest(response, [&](){
HttpServerHelpers::ReturnType ret = HttpServerHelpers::preprocess(p, request, response);
if (ret == HttpServerHelpers::RequestFinished){
return;
}
std::string serviceName = request.getURI();
std::cout << "servicename before: " << serviceName << std::endl;
if (serviceName.find('/') == 0) {
serviceName = serviceName.substr(1);
}
std::string::size_type question = serviceName.find('?');
if (question != std::string::npos) {
serviceName = serviceName.substr(0, question);
}
std::cout << "servicename after: " << serviceName << std::endl;
for (auto i = p.getRequestHandlers().begin(); i != p.getRequestHandlers().end(); i++){
if ((*i)->canHandle(serviceName)){
std::cout << "SPECIAL HANDLING of " << serviceName << std::endl;
(*i)->handle(request, response);
return;
}
}
//std::cout << "service name: " << serviceName << std::endl;
std::istream& rs = request.stream();
std::stringstream outstr;
Poco::StreamCopier::copyStream(rs, outstr);
std::string rsp;
std::string req = outstr.str();
//std::cout << "requset: " << req << std::endl;
//this->handlerProvider.GetHandler()
LTRACE("Json") << "request " << req << LE;
if (auto sHandlerProvider = handlerProvider.lock()) {
sHandlerProvider->getHandler(serviceName)->HandleRequest(req, rsp);
} else {
throw Poco::Exception("Request refused - server destroyed");
}
LTRACE("Json") << "response " << rsp << LE;
//std::cout << "response: " << rsp << std::endl;
response.setContentType("application/json");
response.sendBuffer(rsp.data(), rsp.length());
});
}
示例11:
void server::BasicHandler::handleRequest( Poco::Net::HTTPServerRequest & request,
Poco::Net::HTTPServerResponse & response) {
std::cout << "Received Request!" << std::endl;
response.setContentType("text/txt");
response.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
std::ostream& os = response.send();
os << "12345 this is all pretty weird" << std::endl;
}
示例12: DefaultRequestHandler
void DefaultRequestHandler(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_OK);
response.setContentType(Poco::Net::MediaType("text/plain"));
response.setKeepAlive(true);
auto& os = response.send();
os << "It Works! " << request.getURI() << std::flush;
}
示例13: process
void process(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response, const std::string& path, std::istream& resourceStream, Poco::OSP::Bundle::ConstPtr pBundle)
{
std::string content;
Poco::StreamCopier::copyToString(resourceStream, content);
std::string dateTime = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::HTTP_FORMAT);
Poco::replaceInPlace(content, std::string("$$DATETIME$$"), dateTime);
response.setContentType("text/html");
response.send() << content;
}
示例14: handleRequest
void ShowCaptchaPage::handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
{
m_log->trace("ShowCaptchaPage::handleRequest from "+request.clientAddress().toString());
std::map<std::string,QueryVar> queryvars;
CreateQueryVarMap(request,queryvars);
if(request.getVersion()==Poco::Net::HTTPRequest::HTTP_1_1)
{
response.setChunkedTransferEncoding(true);
}
std::string content="";
if(queryvars.find("UUID")!=queryvars.end())
{
std::string uuid=(*queryvars.find("UUID")).second.GetData();
SQLite3DB::Statement st=m_db->Prepare("SELECT MimeType,PuzzleData FROM tblIntroductionPuzzleRequests WHERE UUID=?;");
st.Bind(0,uuid);
st.Step();
if(st.RowReturned())
{
std::string mime;
std::string b64data;
std::vector<unsigned char> data;
st.ResultText(0,mime);
st.ResultText(1,b64data);
Base64::Decode(b64data,data);
// mime type should be short and have a / in it - otherwise skip
if(mime.size()<50 && mime.find('/')!=std::string::npos)
{
std::string fname(uuid);
if(mime=="image/bmp")
{
fname+=".bmp";
}
else if(mime=="audio/x-wav")
{
fname+=".wav";
}
response.setContentType(mime);
response.setContentLength(data.size());
response.set("Content-Disposition","attachment; filename="+fname);
content+=std::string(data.begin(),data.end());
}
}
}
std::ostream &ostr = response.send();
ostr << content;
}
示例15: prepareResponse
void
Handler::prepareApiResponse(Poco::Net::HTTPServerResponse &resp)
{
prepareResponse(resp);
resp.setContentType("application/json");
resp.set("Cache-Control", "no-cache");
if (!configuration->getAccessControlAllowOriginHeader().empty()) {
resp.set("Access-Control-Allow-Origin", configuration->getAccessControlAllowOriginHeader());
}
}