void handleFilesRequest(const http::Request& request, http::Response* pResponse) { Options& options = session::options(); if (options.programMode() != kSessionProgramModeServer) { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // get prefix and uri std::string prefix = "/files/"; std::string uri = request.uri(); // validate the uri if (prefix.length() >= uri.length() || // prefix longer than uri uri.find(prefix) != 0 || // uri doesn't start with prefix uri.find("..") != std::string::npos) // uri has inavlid char sequence { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // compute path to file int prefixLen = prefix.length(); std::string relativePath = http::util::urlDecode(uri.substr(prefixLen)); if (relativePath.empty()) { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // complete path to file FilePath filePath = module_context::userHomePath().complete(relativePath); // no directory listing available if (filePath.isDirectory()) { pResponse->setError(http::status::NotFound, "No listing available for " + request.uri()); return; } pResponse->setNoCacheHeaders(); pResponse->setFile(filePath, request); }
void handleFileExportRequest(const http::Request& request, http::Response* pResponse) { // see if this is a single or multiple file request std::string file = request.queryParamValue("file"); if (!file.empty()) { // resolve alias and ensure that it exists FilePath filePath = module_context::resolveAliasedPath(file); if (!filePath.exists()) { pResponse->setNotFoundError(request.uri()); return; } // get the name std::string name = request.queryParamValue("name"); if (name.empty()) { pResponse->setError(http::status::BadRequest, "name not specified"); return; } // download as attachment setAttachmentResponse(request, name, filePath, pResponse); } else { handleMultipleFileExportRequest(request, pResponse); } }
void handleFileShow(const http::Request& request, http::Response* pResponse) { // get the file path FilePath filePath(request.queryParamValue("path")); if (!filePath.exists()) { pResponse->setNotFoundError(request.uri()); return; } // send it back pResponse->setCacheWithRevalidationHeaders(); pResponse->setCacheableFile(filePath, request); }
void handleContentRequest(const http::Request& request, http::Response* pResponse) { // get content file info std::string title; FilePath contentFilePath; Error error = contentFileInfo(request.uri(), &title, &contentFilePath); if (error) { pResponse->setError(error); return; } // set private cache forever headers pResponse->setPrivateCacheForeverHeaders(); // set file pResponse->setFile(contentFilePath, request); bool isUtf8 = true; if (boost::algorithm::starts_with(contentFilePath.mimeContentType(), "text/")) { // If the content looks like valid UTF-8, assume it is. Otherwise, assume // it's the system encoding. std::string contents; error = core::readStringFromFile(contentFilePath, &contents); if (!error) { for (std::string::iterator pos = contents.begin(); pos != contents.end(); ) { error = string_utils::utf8Advance(pos, 1, contents.end(), &pos); if (error) { isUtf8 = false; break; } } } } // reset content-type with charset pResponse->setContentType(contentFilePath.mimeContentType() + std::string("; charset=") + (isUtf8 ? "UTF-8" : ::locale2charset(NULL))); // set title header pResponse->setHeader("Title", title); }
void RequestHandlerGET::HandleRequest( const http::Request& request, http::Response* response) const { string path; string rest; string fragment; if (!DecodeURL(request.uri(), &path, &rest, &fragment)) { *response = Response::StockResponse(Response::BAD_REQUEST); return; } LOG_DEBUG(logger_, "path: " << path) LOG_DEBUG(logger_, "rest: " << rest) LOG_DEBUG(logger_, "fragment: " << fragment) string full_path = root_directory_ + path; LOG_DEBUG(logger_, full_path) CreateResponse(full_path, response); }