/* Serve downloadable file. */ void serveDownloadableFile(int client, const char *filename, const char *displayFilename, const char * type) { FILE *resource = NULL; char buf[1024]; size_t len; STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); len = snprintf(buf, sizeof(buf), "Content-Type: %s\r\n",type); send(client, buf, len, 0); len = snprintf(buf, sizeof(buf), "Content-Disposition: attachment; filename=\"%s\"\r\n",displayFilename); send(client, buf, len, 0); STATIC_SEND(client, "\r\n", 0); resource = fopen(filename, "r"); if (resource == NULL) notFound(client); else { cat(client, resource); } fclose(resource); }
void writeStandardHeaders(int client) { STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); STATIC_SEND(client, "Content-Type: text/html\r\n", 0); STATIC_SEND(client, "\r\n", 0); }
void sendHeadersTypeEncoding(Request request,const char *type, const char * encoding) { int client=request.client; STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); FDPRINTF(client, "Content-Type: %s\r\n", type,0); if (encoding!=NULL) { FDPRINTF(client, "Content-Encoding: %s\r\n", encoding,0); } STATIC_SEND(client, "\r\n", 0); }
void gzw_sendHeadersType(gzwRequest gzwr,const char *type) { int client=gzwr.request.client; STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); FDPRINTF(client, "Content-Type: %s\r\n", type,0); if (gzwr.gzenabled) { FDPRINTF(client, "Content-Encoding: %s\r\n", ENCODING_GZIP,0); } STATIC_SEND(client, "Vary: Accept-Encoding\r\n", 0); STATIC_SEND(client, "\r\n", 0); }
void gzw_sendHeadersFromString(gzwRequest gzwr,const char *headerString) { int client=gzwr.request.client; if (gzwr.gzenabled) { FDPRINTF(client, "Content-Encoding: %s\r\n", ENCODING_GZIP,0); } FDPRINTF(client, "%s", headerString,0); STATIC_SEND(client, "\r\n", 0); }
void unimplemented(int client) { STATIC_SEND(client, "HTTP/1.0 501 Method Not Implemented\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); STATIC_SEND(client, "Content-Type: text/html\r\n", 0); STATIC_SEND(client, "\r\n", 0); STATIC_SEND(client, "<HTML><HEAD><TITLE>Method Not Implemented\r\n", 0); STATIC_SEND(client, "</TITLE></HEAD>\r\n", 0); STATIC_SEND(client, "<BODY><P>HTTP request method not supported.\r\n", 0); STATIC_SEND(client, "</P></BODY></HTML>\r\n", 0); }
/* Serve downloadable file. */ void serveDownloadableFile(int client, const char *filename, const char *displayFilename, const char * type) { FILE *resource = NULL; STATIC_SEND(client, "HTTP/1.0 200 OK\r\n", 0); STATIC_SEND(client, SERVER_STRING, 0); FDPRINTF(client, "Content-Type: %s\r\n",type); FDPRINTF(client, "Content-Disposition: attachment; filename=\"%s\"\r\n",displayFilename); STATIC_SEND(client, "\r\n", 0); resource = fopen(filename, "r"); if (resource == NULL) { not_found(client); } else { cat(client, resource); } fclose(resource); }
void server(Request * request, Response * response) { // open sqlite3 database "URL_Records" rc = sqlite3_open("URL_Records.db", &db); // if db exist it will open otherwise create a db if (rc) { fprintf(stderr, "sqlite db connectivity error."); exit(0); } //query to create "Records" table if not exists. sql = "CREATE TABLE if not exists Records( " "ID INTEGER PRIMARY KEY AUTOINCREMENT," "Hash CHAR(5) NOT NULL,URL TEXT NOT NULL);"; rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); //Execute the sql statement. if (rc != SQLITE_OK) { fprintf(stderr, "SQL table creating error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } printf("%s %s\n", request->method, request->reqStr); if (routeRequest(request, response, "/shorten", form) || routeRequest(request, response, "/", form)) { //Request to shorten the URL } else { response->apiFlags |= API_FLAGS_DONT_SET_HEADER_BEFORE_SENDING; char * queryPath = getQueryPath(request); char longUrl[4096]; getLongUrl(queryPath, longUrl); STATIC_SEND(response->fd, "HTTP/1.1 301 Moved Permanently\r\n"); resPrintf(response, "Location: %s\r\n", longUrl); STATIC_SEND(response->fd, "\r\n"); free(queryPath); } sqlite3_close(db); }
void client_error(int fd, int status, char *msg, char *longmsg){ nprintf(fd, "HTTP/1.1 %d %s\r\n", status, msg); STATIC_SEND(fd, SERVER_STRING, 0); nprintf(fd, "Content-length: %lu\r\n\r\n", strlen(longmsg)); nprintf(fd, "%s", longmsg); }
void sendHeadersFromString(Request request, const char *headerString) { int client = request.client; FDPRINTF(client, "%s", headerString, 0); STATIC_SEND(client, "\r\n", 0); }