// Sends a file to the server void sendFile(HTTPClient* client, string filename) { HTTPRequest request; HTTPResponse response; // Send a HEAD request request.method = "HEAD"; request.path = "/" + filename; response = client->send(request); if (response.status == 200) { // The file exists. Compare MD5 checksums // TODO: Make a local MD5 checksums cache when this get's multithreaded. if (WebUtil::MD5(filename) == response.getHeader("Content-MD5")) { return; } } request.method = "PUT"; request.path = "/" + filename; FILE* f = fopen(filename.c_str(),"r"); if (f == NULL) { throw_exception("Couldn't read " + filename); } request.setBody(f); request.addHeader("Content-type", WebUtil::pathToMimetype(filename)); request.addHeader("Content-length", WebUtil::filesize(filename)); response = client->send(request); fclose(f); }
// Creates a dir on the server void createDir(HTTPClient* client, string dirname) { HTTPRequest request; HTTPResponse response; request.method = "MKCOL"; request.path = "/" + dirname; request.addHeader("Content-length", 0); request.setBody(""); response = client->send(request); }
void OAuth::addAuthorizationHeadersToRequest(HTTPRequest& r) { std::string signature = createSignature(r); HTTPParameters params = collectParameters(r); params.add(HTTPParameter("oauth_signature", signature)); params.percentEncode(); std::string header = "OAuth "; OAUTH_CONCAT_HEADER(header, "oauth_callback", true); OAUTH_CONCAT_HEADER(header, "oauth_consumer_key", true); OAUTH_CONCAT_HEADER(header, "oauth_nonce", true); OAUTH_CONCAT_HEADER(header, "oauth_signature", true); OAUTH_CONCAT_HEADER(header, "oauth_signature_method", true); OAUTH_CONCAT_HEADER(header, "oauth_timestamp", true); OAUTH_CONCAT_HEADER(header, "oauth_token", true); OAUTH_CONCAT_HEADER(header, "oauth_version", false); r.addHeader(HTTPHeader("Authorization", header)); }
int Webserver::process(FILE* f) { char buf[4096]; char *method; char *path; char *protocol; if (!fgets(buf, sizeof(buf), f)) return -1; printf("%s", buf); char* bufp = buf; method = strsep(&bufp, " "); path = strsep(&bufp, " "); protocol = strsep(&bufp, "\r"); if (!method || !path || !protocol) return -1; // Extract request HTTPRequest request; request.method = string(method); request.path = string(path); request.readHeaders(f); request.writeHeaders(stdout); request.readParams(f); request.setBody(f); // Find and execute action Action* action = actions[path]; if (action == NULL) { action = file_action; } HTTPResponse response = action->execute(request); response.addHeader("Server", string("RayGay Renderslave ") + string(VERSION)); request.addHeader("Date", WebUtil::formatDate(time(NULL))); // Send response fseek(f, 0, SEEK_CUR); // Force change of stream direction fprintf(f, "HTTP/1.0 %d %s\r\n", response.status, response.statusString().c_str()); response.writeHeaders(f); response.writeBody(f); return 0; }