int main (int argc, char** argv) { CDServer* server; int opt; bool noFork = false; bool debugging = false; char* config = NULL; static const char* configSearchPath[] = { "%s/.craftd/craftd.conf", // %s is replaced with $HOME "/etc/craftd/craftd.conf", "/usr/local/etc/craftd/craftd.conf", "craftd.conf", // Current working directory (for devs) NULL }; CDDefaultLogger = CDConsoleLogger; LOG(LOG_INFO, "%s " CRAFTD_NOTICE_MESSAGE, argv[0]); while ((opt = getopt(argc, argv, "c:dhnv")) != -1) { switch (opt) { case 'd': { // debugging mode debugging = true; } break; case 'v': { // print version exit(EXIT_SUCCESS); // Version header already printed } break; case 'n': { // don't fork or daemonize, use stdout for logging noFork = true; } break; case 'c': { // use the specified config file config = optarg; } break; case 'h': // print help message default: { fprintf(stderr, "\nUsage: %s [OPTION]...\n" "-c <conf file> specify a conf file location\n" "-d enable verbose debugging messages\n" "-h display this help and exit\n" "-n don't fork/daemonize (overrides config file)\n" "-v output version information and exit\n" "\n" "For complete documentation, visit the wiki.\n\n", argv[0]); exit((opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE); } } } if (!config) { char path[FILENAME_MAX] = { 0 }; const char** current = configSearchPath; do { snprintf(path, FILENAME_MAX, *current, getenv("HOME")); current++; } while (*current != NULL && !CD_PathExists(path)); if (!CD_PathExists(path)) { CD_abort("The config file could not be found"); } else { config = path; } } if (!CD_IsReadable(config)) { CD_abort("%s could not be read", config); } #ifdef WIN32 evthread_use_windows_threads(); #else evthread_use_pthreads(); #endif if (debugging) { evthread_enable_lock_debuging(); } CDMainServer = server = CD_CreateServer(config); if (!server) { CD_abort("Server couldn't be instantiated"); } /* By default, mask debugging messages */ if (!debugging) { server->logger.setlogmask(LOG_MASK(LOG_DEBUG)); } CD_RunServer(server); LOG(LOG_INFO, "Exiting."); LOG_CLOSE(); CD_DestroyServer(server); }
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); } } }