Compare commits
	
		
			2 Commits
		
	
	
		
			03c6669c59
			...
			2be821f324
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 2be821f324 | |
|  | 466115bc39 | 
|  | @ -4,6 +4,7 @@ | |||
| #include "404.hpp" | ||||
| 
 | ||||
| int not_found(struct http_request* req) { | ||||
|     http_response_header(req, "Content-Type", "text/html"); | ||||
|     http_response(req, 404, asset_404_html, asset_len_404_html); | ||||
|     return KORE_RESULT_OK; | ||||
| } | ||||
|  |  | |||
|  | @ -19,13 +19,13 @@ void kore_parent_configure(int argc, char* argv[]) { | |||
|     } | ||||
|     config_fstream.close(); | ||||
|     if (config_fstream.fail() && !config_fstream.eof()) { | ||||
|         kore_log(LOG_NOTICE, "fail bit set when reading config"); | ||||
|         kore_log(LOG_ERR, "fail bit set when reading config"); | ||||
|         exit(1); | ||||
|     } | ||||
|     kore_json json; | ||||
|     kore_json_init(&json, config_str.c_str(), config_str.length()); | ||||
|     if (kore_json_parse(&json) != KORE_RESULT_OK) { | ||||
|         kore_log(LOG_NOTICE, "failed to parse json: %s", kore_json_strerror(&json)); | ||||
|         kore_log(LOG_ERR, "failed to parse json: %s", kore_json_strerror(&json)); | ||||
|         exit(1); | ||||
|     } | ||||
|     kore_json_item* redis_host_json = kore_json_find_string(json.root, "redis_host"); | ||||
|  | @ -42,16 +42,16 @@ void kore_parent_configure(int argc, char* argv[]) { | |||
| 
 | ||||
| void kore_worker_configure() { | ||||
|     if (config.redis_host && config.redis_port) { | ||||
|         kore_log(LOG_NOTICE, "connecting to redis instance at %s:%d", (*config.redis_host).c_str(), (*config.redis_port)); | ||||
|         kore_log(LOG_INFO, "connecting to redis instance at %s:%d", (*config.redis_host).c_str(), (*config.redis_port)); | ||||
|         struct timeval timeout = {1, 0}; | ||||
|         redis = redisConnectWithTimeout((*config.redis_host).c_str(), (*config.redis_port), timeout); | ||||
|         if (!redis || redis->err) { | ||||
|             if (redis) { | ||||
|                 kore_log(LOG_NOTICE, "failed to connect to redis: %s", redis->errstr); | ||||
|                 kore_log(LOG_ERR, "failed to connect to redis: %s", redis->errstr); | ||||
|                 redisFree(redis); | ||||
|                 redis = nullptr; | ||||
|             } else { | ||||
|                 kore_log(LOG_NOTICE, "failed to connect to redis"); | ||||
|                 kore_log(LOG_ERR, "failed to connect to redis"); | ||||
|             } | ||||
|             kore_shutdown(); | ||||
|             exit(1); | ||||
|  | @ -60,9 +60,9 @@ void kore_worker_configure() { | |||
|             redisReply* reply = (redisReply*)redisCommand(redis, "AUTH %s", (*config.redis_password).c_str()); | ||||
|             if (!reply || reply->type == REDIS_REPLY_ERROR) { | ||||
|                 if (!reply) { | ||||
|                     kore_log(LOG_NOTICE, "received nullptr while authenticating to redis"); | ||||
|                     kore_log(LOG_ERR, "received nullptr while authenticating to redis"); | ||||
|                 } else { | ||||
|                     kore_log(LOG_NOTICE, "received error while authenticating to redis: %s", reply->str); | ||||
|                     kore_log(LOG_ERR, "received error while authenticating to redis: %s", reply->str); | ||||
|                     freeReplyObject(reply); | ||||
|                 } | ||||
|                 redisFree(redis); | ||||
|  |  | |||
|  | @ -28,11 +28,11 @@ static bool send_hget_and_http_resp(struct http_request* req, const char* id, bo | |||
|     redisReply* reply = (redisReply*)redisCommand(redis, "HGET %s %s", keys.key.c_str(), keys.field.c_str()); | ||||
|     if (!reply) { | ||||
|         redis_mutex.unlock(); | ||||
|         kore_log(LOG_NOTICE, "received nullptr while sending HGET to redis"); | ||||
|         kore_log(LOG_WARNING, "received nullptr while sending HGET to redis"); | ||||
|         return false; | ||||
|     } | ||||
|     if (reply->type == REDIS_REPLY_ERROR) { | ||||
|         kore_log(LOG_NOTICE, "received error while sending HGET to redis: %s", reply->str); | ||||
|         kore_log(LOG_WARNING, "received error while sending HGET to redis: %s", reply->str); | ||||
|         freeReplyObject(reply); | ||||
|         redis_mutex.unlock(); | ||||
|         return false; | ||||
|  | @ -50,6 +50,7 @@ static bool send_hget_and_http_resp(struct http_request* req, const char* id, bo | |||
|             error = "404: album not found"; | ||||
|         } | ||||
|         std::string error_page = build_error_page(error); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 404, error_page.c_str(), error_page.length()); | ||||
|         return true; | ||||
|     } | ||||
|  | @ -58,7 +59,7 @@ static bool send_hget_and_http_resp(struct http_request* req, const char* id, bo | |||
|     freeReplyObject(reply); | ||||
|     redis_mutex.unlock(); | ||||
|     if (error) { | ||||
|         kore_log(LOG_NOTICE, "received error while parsing packed album: %s", (*error).c_str()); | ||||
|         kore_log(LOG_WARNING, "received error while parsing packed album: %s", (*error).c_str()); | ||||
|         return false; | ||||
|     } | ||||
|     send_album_page(req, std::move(album)); | ||||
|  | @ -89,8 +90,9 @@ static int album_or_image_start(struct http_request* req) { | |||
|     struct kore_curl* client = (kore_curl*)http_state_create(req, sizeof(*client), NULL); | ||||
|     if (!kore_curl_init(client, api_url.c_str(), KORE_CURL_ASYNC)) { | ||||
|         http_state_cleanup(req); | ||||
|         kore_log(LOG_NOTICE, "failed to initialize curl client"); | ||||
|         kore_log(LOG_ERR, "failed to initialize curl client"); | ||||
|         std::string error_page = build_error_page("Failed to initialize curl client"); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  | @ -109,11 +111,11 @@ static void send_hset(const char* id, bool is_album, const char* packed) { | |||
|     redisReply* reply = (redisReply*)redisCommand(redis, "HSET %s %s %s", keys.key.c_str(), keys.field.c_str(), packed); | ||||
|     if (!reply) { | ||||
|         redis_mutex.unlock(); | ||||
|         kore_log(LOG_NOTICE, "received nullptr while sending HSET to redis"); | ||||
|         kore_log(LOG_WARNING, "received nullptr while sending HSET to redis"); | ||||
|         return; | ||||
|     } | ||||
|     if (reply->type == REDIS_REPLY_ERROR) { | ||||
|         kore_log(LOG_NOTICE, "received error while sending HSET to redis: %s", reply->str); | ||||
|         kore_log(LOG_WARNING, "received error while sending HSET to redis: %s", reply->str); | ||||
|         freeReplyObject(reply); | ||||
|         redis_mutex.unlock(); | ||||
|         return; | ||||
|  | @ -139,6 +141,7 @@ static int album_or_image_end(struct http_request* req) { | |||
|         kore_curl_cleanup(client); | ||||
|         http_state_cleanup(req); | ||||
|         std::string error_page = build_error_page(error.c_str()); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  | @ -146,13 +149,14 @@ static int album_or_image_end(struct http_request* req) { | |||
|     kore_json json; | ||||
|     kore_json_init(&json, body, strlen(body)); | ||||
|     if (kore_json_parse(&json) != KORE_RESULT_OK) { | ||||
|         kore_log(LOG_NOTICE, "failed to parse json: %s", kore_json_strerror(&json)); | ||||
|         kore_log(LOG_ERR, "failed to parse json: %s", kore_json_strerror(&json)); | ||||
|         std::string error = "Failed to parse response: "; | ||||
|         error.append(kore_json_strerror(&json)); | ||||
|         kore_json_cleanup(&json); | ||||
|         kore_curl_cleanup(client); | ||||
|         http_state_cleanup(req); | ||||
|         std::string error_page = build_error_page(error.c_str()); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  | @ -174,6 +178,7 @@ static int album_or_image_end(struct http_request* req) { | |||
|                     send_hset(id, is_album, ""); | ||||
|                 } | ||||
|                 std::string error_page = build_error_page(error.c_str()); | ||||
|                 http_response_header(req, "Content-Type", "text/html"); | ||||
|                 http_response(req, 404, error_page.c_str(), error_page.length()); | ||||
|                 return HTTP_STATE_COMPLETE; | ||||
|             } | ||||
|  | @ -191,7 +196,7 @@ static int album_or_image_end(struct http_request* req) { | |||
|             if (error_detail_json) { | ||||
|                 error_detail = error_detail_json->data.string; | ||||
|             } | ||||
|             kore_log(LOG_NOTICE, "received error from api: %s: %s", error_code, error_detail); | ||||
|             kore_log(LOG_ERR, "received error from api: %s: %s", error_code, error_detail); | ||||
|             std::string to_append; | ||||
|             if (needs_newline) { | ||||
|                 to_append.append("\n"); | ||||
|  | @ -208,6 +213,7 @@ static int album_or_image_end(struct http_request* req) { | |||
|         kore_curl_cleanup(client); | ||||
|         http_state_cleanup(req); | ||||
|         std::string error_page = build_error_page(error.c_str(), true); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  | @ -217,8 +223,9 @@ static int album_or_image_end(struct http_request* req) { | |||
|     kore_curl_cleanup(client); | ||||
|     http_state_cleanup(req); | ||||
|     if (error) { | ||||
|         kore_log(LOG_NOTICE, "%s", (*error).c_str()); | ||||
|         kore_log(LOG_ERR, "%s", (*error).c_str()); | ||||
|         std::string error_page = build_error_page((*error).c_str()); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  |  | |||
|  | @ -28,8 +28,9 @@ static int proxy_start(struct http_request* req) { | |||
|     struct kore_curl* client = (kore_curl*)http_state_create(req, sizeof(*client), NULL); | ||||
|     if (!kore_curl_init(client, url.c_str(), KORE_CURL_ASYNC)) { | ||||
|         http_state_cleanup(req); | ||||
|         kore_log(LOG_NOTICE, "failed to initialize curl client"); | ||||
|         kore_log(LOG_ERR, "failed to initialize curl client"); | ||||
|         std::string error_page = build_error_page("Failed to initialize curl client"); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  | @ -69,6 +70,7 @@ static int proxy_end(struct http_request* req) { | |||
|         kore_curl_cleanup(client); | ||||
|         http_state_cleanup(req); | ||||
|         std::string error_page = build_error_page(error.c_str()); | ||||
|         http_response_header(req, "Content-Type", "text/html"); | ||||
|         http_response(req, 500, error_page.c_str(), error_page.length()); | ||||
|         return HTTP_STATE_COMPLETE; | ||||
|     } | ||||
|  |  | |||
|  | @ -396,6 +396,7 @@ int send_album_page(struct http_request* req, Album album) { | |||
|         } | ||||
|     } | ||||
|     response.append(ALBUM_PAGE_END); | ||||
|     http_response_header(req, "Content-Type", "text/html"); | ||||
|     http_response(req, 200, response.c_str(), response.length()); | ||||
|     return HTTP_STATE_COMPLETE; | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue