MCString MC_StringSanitize (MCString self) { CDString* result = CD_CreateString(); assert(self); for (size_t i = 0, ie = CD_StringLength(self); i < ie; i++) { bool has = false; CDString* ch = CD_CharAt(self, i); for (size_t h = 0, he = cd_UTF8_strlen(MCCharset); h < he; h++) { const char* che = &MCCharset[cd_UTF8_offset(MCCharset, h)]; if (strncmp(CD_StringContent(ch), che, CD_StringSize(ch)) == 0) { has = true; break; } } if (i == ie - 2 && strncmp(CD_StringContent(ch), "§", 2) == 0){ CD_DestroyString(ch); break; } if (has || strncmp(CD_StringContent(ch), "§", 2) == 0) { CD_AppendString(result, ch); } else { CD_AppendCString(result, "?"); } CD_DestroyString(ch); } cd_UpdateLength(self); return result; }
static void cd_StaticRequest (struct evhttp_request* request, CDServer* server) { int error = HTTP_OK; const char* message = "OK"; const char* uri = evhttp_request_get_uri(request); struct evhttp_uri* decoded = evhttp_uri_parse(uri); if (evhttp_request_get_command(request) != EVHTTP_REQ_GET) { error = HTTP_BADMETHOD; message = "Invalid request method"; goto end; } if (!decoded || strstr(evhttp_uri_get_path(decoded), "..")) { error = HTTP_BADREQUEST; message = "Bad request"; goto end; } DO { CDString* path = CD_CreateStringFromFormat("%s/%s", server->config->cache.httpd.root, evhttp_uri_get_path(decoded) ? evhttp_uri_get_path(decoded) : "index.html"); if (CD_IsDirectory(CD_StringContent(path))) { CD_AppendCString(path, "/index.html"); } if (!CD_IsReadable(CD_StringContent(path))) { error = HTTP_NOTFOUND; message = "File not found"; CD_DestroyString(path); goto end; } struct evbuffer* buffer = evbuffer_new(); int fd = open(CD_StringContent(path), O_RDONLY); evhttp_add_header(evhttp_request_get_output_headers(request), "Content-Type", cd_GuessContentType(CD_StringContent(path))); evbuffer_add_file(buffer, fd, 0, CD_FileSize(CD_StringContent(path))); evhttp_send_reply(request, HTTP_OK, "OK", buffer); evbuffer_free(buffer); CD_DestroyString(path); } end: { if (decoded) { evhttp_uri_free(decoded); } if (error != HTTP_OK) { evhttp_send_error(request, error, message); } } }