void PostHightScore::generateResponse(Poco::Net::HTTPServerRequest& inRequest, Poco::Net::HTTPServerResponse& outResponse) { std::string requestBody; inRequest.stream() >> requestBody; Args args; GetArgs(requestBody, args); std::string name = URIDecode(GetArg(args, "name")); const std::string & score = GetArg(args, "score"); Statement insert(getSession()); insert << "INSERT INTO HighScores VALUES(NULL, strftime('%s', 'now'), ?, ?)", use(name), use(score); insert.execute(); // Return an URL instead of a HTML page. // This is because the client is the JavaScript application in this case. std::string body = ResourceManager::Instance().getResourceLocation(GetResourceId()); outResponse.setContentLength(body.size()); outResponse.send() << body; }
inline std::size_t URIDecodeInPlace(std::string &URI) { return URIDecode(URI, URI); }
int main(int argc, char *argv[]) { POINT mouse; //while (true) { GetCursorPos(&mouse); printf("\r%d-%d ", mouse.x, mouse.y); } return interlace_RGB(); return matrix(); // char URI[] = "//mnt/sdcard/%E9%98%BF%E4%B8%BD%E4%BA%9A%E5%A8%9C%E5%90%89%E5%88%A9%E6%96%AF-%20Shake%E8%8B%B9%E6%9E%9C.3dv"; char URI[] = "//mnt/sdcard/HELLO!.3dv"; char decoded_URI[1024]; URIDecode(URI, decoded_URI, sizeof(decoded_URI)); char k = 0xef; bool a = k == 0xef; const int frame_w = 1920; const int frame_h = 1080; setlocale(LC_ALL, "CHS"); if (argc < 4) { printf("usage: %s <image file> <subtitle file> <time>\n", argv[0]); exit(1); } char *imgfile = argv[1]; char *subfile = argv[2]; double tm = strtod(argv[3], 0); FILE * f = fopen(subfile, "rb"); fseek(f, 0, SEEK_END); int file_size = ftell(f); fseek(f, 0, SEEK_SET); char *src = (char*)malloc(file_size); char *utf8 = (char*)malloc(file_size*3); fread(src, 1, file_size, f); fclose(f); int utf8_size = ConvertToUTF8(src, file_size, utf8, file_size*3); ass_library = ass_library_init(); if (!ass_library) { printf("ass_library_init failed!\n"); exit(1); } //ass_set_message_cb(ass_library, msg_callback, NULL); //ass_set_extract_fonts(ass_library, 0); //ass_set_style_overrides(ass_library, NULL); ass_renderer = ass_renderer_init(ass_library); if (!ass_renderer) { printf("ass_renderer_init failed!\n"); exit(1); } ass_set_frame_size(ass_renderer, frame_w, frame_h); ass_set_font_scale(ass_renderer, 1.0); //ass_set_hinting(ass_renderer, ASS_HINTING_NORMAL); ass_set_fonts(ass_renderer, "Arial", "Sans", 1, "Z:\\fonts.conf", 1); ASS_Track *track = ass_read_memory(ass_library, utf8, utf8_size, NULL); free(src); free(utf8); if (!track) { printf("track init failed!\n"); return 1; } ASS_Image *img = NULL; int n = 0; int changed = 0; image_t *frame = gen_image(frame_w, frame_h); int n2 = 0; int l = GetTickCount(); timeBeginPeriod(1); for(int i=0; i<int(tm*1000); i+=40) { img = ass_render_frame(ass_renderer, track, i, &changed); if (n==0) l = GetTickCount(); if (changed && img) { int l = timeGetTime(); n++; memset(frame->buffer, 63, frame->stride * frame->height); blend(frame, img); wchar_t pathname[MAX_PATH]; wsprintfW(pathname, L"Z:\\ass%02d.bmp", n); save_bitmap((DWORD*)frame->buffer, pathname, frame_w, frame_h); //printf("\rrender cost %dms.\t\t\n", timeGetTime()-l); } n2 ++; if (i%10000 == 0) printf("\r%d/%d ms rendered, %d frame output.", i, int(tm*1000), n); } ass_free_track(track); ass_renderer_done(ass_renderer); ass_library_done(ass_library); free(frame->buffer); free(frame); return 0; }
void RequestHandler::handle_request(const http::Request &req, http::Reply &reply) { // parse command try { std::string request; URIDecode(req.uri, request); // deactivated as GCC apparently does not implement that, not even in 4.9 // std::time_t t = std::time(nullptr); // SimpleLogger().Write() << std::put_time(std::localtime(&t), "%m-%d-%Y %H:%M:%S") << // " " << req.endpoint.to_string() << " " << // req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << // req.agent << ( 0 == req.agent.length() ? "- " :" ") << request; time_t ltime; struct tm *Tm; ltime = time(nullptr); Tm = localtime(<ime); SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-" << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-" << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "") << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "") << Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "") << Tm->tm_sec << " " << req.endpoint.to_string() << " " << req.referrer << (0 == req.referrer.length() ? "- " : " ") << req.agent << (0 == req.agent.length() ? "- " : " ") << request; RouteParameters route_parameters; APIGrammarParser api_parser(&route_parameters); auto it = request.begin(); const bool result = boost::spirit::qi::parse(it, request.end(), api_parser); if (!result || (it != request.end())) { reply = http::Reply::StockReply(http::Reply::badRequest); reply.content.clear(); const int position = std::distance(request.begin(), it); JSON::Object json_result; json_result.values["status"] = 400; std::string tmp_position_string = IntToString(position); std::string message = ("Query string malformed close to position " + IntToString(position)); json_result.values["status_message"] = message; JSON::render(reply.content, json_result); } else { // parsing done, lets call the right plugin to handle the request BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed"); if (!route_parameters.jsonp_parameter.empty()) { const std::string json_p = (route_parameters.jsonp_parameter + "("); reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); } routing_machine->RunQuery(route_parameters, reply); // set headers, still ugly and should be reworked reply.headers.resize(3); if ("gpx" == route_parameters.output_format) { reply.headers[1].name = "Content-Type"; reply.headers[1].value = "application/gpx+xml; charset=UTF-8"; reply.headers[2].name = "Content-Disposition"; reply.headers[2].value = "attachment; filename=\"route.gpx\""; } else if (!route_parameters.jsonp_parameter.empty()) { reply.content.push_back(')'); reply.headers[1].name = "Content-Type"; reply.headers[1].value = "text/javascript"; reply.headers[2].name = "Content-Disposition"; reply.headers[2].value = "attachment; filename=\"response.js\""; } else { reply.headers[1].name = "Content-Type"; reply.headers[1].value = "application/x-javascript"; reply.headers[2].name = "Content-Disposition"; reply.headers[2].value = "attachment; filename=\"response.json\""; } reply.headers[0].name = "Content-Length"; reply.headers[0].value = IntToString(reply.content.size()); return; } } catch (const std::exception &e) { reply = http::Reply::StockReply(http::Reply::internalServerError); SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what() << ", uri: " << req.uri; return; } }
void RequestHandler::handle_request(const http::Request &req, http::Reply &reply) { // parse command try { std::string request; URIDecode(req.uri, request); // deactivated as GCC apparently does not implement that, not even in 4.9 // std::time_t t = std::time(nullptr); // SimpleLogger().Write() << std::put_time(std::localtime(&t), "%m-%d-%Y %H:%M:%S") << // " " << req.endpoint.to_string() << " " << // req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << // req.agent << ( 0 == req.agent.length() ? "- " :" ") << request; time_t ltime; struct tm *Tm; ltime = time(nullptr); Tm = localtime(<ime); // log timestamp SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-" << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-" << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "") << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "") << Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "") << Tm->tm_sec << " " << req.endpoint.to_string() << " " << req.referrer << (0 == req.referrer.length() ? "- " : " ") << req.agent << (0 == req.agent.length() ? "- " : " ") << request; RouteParameters route_parameters; APIGrammarParser api_parser(&route_parameters); auto iter = request.begin(); const bool result = boost::spirit::qi::parse(iter, request.end(), api_parser); // check if the was an error with the request if (!result || (iter != request.end())) { reply = http::Reply::StockReply(http::Reply::badRequest); reply.content.clear(); const unsigned position = static_cast<unsigned>(std::distance(request.begin(), iter)); JSON::Object json_result; json_result.values["status"] = 400; std::string message = "Query string malformed close to position "; message += UintToString(position); json_result.values["status_message"] = message; JSON::render(reply.content, json_result); return; } // parsing done, lets call the right plugin to handle the request BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed"); if (!route_parameters.jsonp_parameter.empty()) { // prepend response with jsonp parameter const std::string json_p = (route_parameters.jsonp_parameter + "("); reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); } routing_machine->RunQuery(route_parameters, reply); if (!route_parameters.jsonp_parameter.empty()) { // append brace to jsonp response reply.content.push_back(')'); } reply.headers.emplace_back("Access-Control-Allow-Origin", "*"); reply.headers.emplace_back("Access-Control-Allow-Methods", "GET"); reply.headers.emplace_back("Access-Control-Allow-Headers", "X-Requested-With, Content-Type"); // set headers reply.headers.emplace_back("Content-Length", UintToString(static_cast<unsigned>(reply.content.size()))); if ("gpx" == route_parameters.output_format) { // gpx file reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8"); reply.headers.emplace_back("Content-Disposition", "attachment; filename=\"route.gpx\""); } else if (route_parameters.jsonp_parameter.empty()) { // json file reply.headers.emplace_back("Content-Type", "application/json; charset=UTF-8"); reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.json\""); } else { // jsonp reply.headers.emplace_back("Content-Type", "text/javascript; charset=UTF-8"); reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.js\""); } } catch (const std::exception &e) { reply = http::Reply::StockReply(http::Reply::internalServerError); SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what() << ", uri: " << req.uri; return; } }