diff --git a/httpproject/origin/httpclient.c b/httpproject/origin/httpclient.c index a6871b55b9f8e964c30a2050b945af2a082afc1c..0d4f771544078fa7dd3a345693f5bb5d9243efb5 100644 --- a/httpproject/origin/httpclient.c +++ b/httpproject/origin/httpclient.c @@ -56,7 +56,17 @@ int main(int argc, char *argv[]){ char* recv_data = HandleGetMethod(httpclient, input_data); if(recv_data){ - printf("%s\n", (char*)recv_data); + json_t* recv_object = json_loads(recv_data, JSON_DECODE_ANY, NULL); + + void *iter = json_object_iter(recv_object); + + printf("{"); + while(iter){ + json_t* value = json_object_iter_value(iter); + printf("%s : %s", json_object_iter_key(iter), json_string_value(value)); + iter = json_object_iter_next(recv_object, iter); + } + printf("}\n"); free(recv_data); } diff --git a/httpproject/origin/httpserver.c b/httpproject/origin/httpserver.c index 1476ee5de6a43019284ff39ee3b555993c4e543d..36757c7538cd98c7cc18ea629ee7b380b561021e 100644 --- a/httpproject/origin/httpserver.c +++ b/httpproject/origin/httpserver.c @@ -11,13 +11,6 @@ #define GET 0 #define POSTBUFFERSIZE 512 -typedef struct connection_info_struct -{ - int connectiontype; - char *answer_object; - struct MHD_PostProcessor *postprocessor; -}connection_info_struct; - json_t* LoadJsonFile(); static int AnswerToConnection(void *cls, struct MHD_Connection *connection, @@ -27,20 +20,22 @@ static int AnswerToConnection(void *cls, struct MHD_Connection *connection, static int SendData(struct MHD_Connection *connection, const char *data); -static int IteratePost(void *coninfo_cls, enum MHD_ValueKind kind, const char *key, - const char *filename, const char *content_type, - const char *transfer_encoding, const char *data, uint64_t off, - size_t size); - -static void RequestCompleted(void *cls, struct MHD_Connection *connection, - void **con_cls, enum MHD_RequestTerminationCode toe); - char** ParseUri(int *size, const char *url); void FreeParseResult(char **parse_result, int size); char* GetSongInfoFromGetMethod(int index, json_t* json_data, char **parse_result, int size); +char* GetSongInformation(char **parse_result, int size, char *song_name); + +int GetSingerFromJsonObject(json_t *object, char *song_name); + +int GetLyricFromJsonObject(json_t *object, char *song_name); + +json_t* FindDataFromJsonArray(json_t *array, char *key_data); + +json_t* FindDataFromJsonObject(json_t *object, char *key_data); + int InitializeSignal(); void Sigint_Handler(); @@ -68,8 +63,7 @@ int main(int argc, char *argv[]){ printf("port_number : %d\n", port_number); daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port_number, NULL, NULL, - &AnswerToConnection, NULL,MHD_OPTION_NOTIFY_COMPLETED, RequestCompleted, - NULL, MHD_OPTION_END); + &AnswerToConnection, NULL, MHD_OPTION_END); if(daemon == NULL){ printf("daemon start fail\n"); @@ -80,11 +74,6 @@ int main(int argc, char *argv[]){ getchar(); - /* while(!exitstatus) - { - sleep(1); - } */ - MHD_stop_daemon(daemon); json_decref(json_root); @@ -112,62 +101,29 @@ static int AnswerToConnection(void *cls, struct MHD_Connection *connection, printf("encoding : %s\n", encoding); printf("url : %s method : %s data : %s size : %u\n", url, method, upload_data, *upload_data_size); - /*if(NULL == *con_cls){ - struct connection_info_struct *con_info = malloc(sizeof(struct connection_info_struct)); - - if(con_info == NULL){ - return MHD_NO; - } - - con_info->answer_object = NULL; + if(0 == strcmp(method, "GET")){ + int size = 0; + char **parse_result = ParseUri(&size, url); + int i = 0; - if(0 == strcmp(method, "POST")){ - con_info->postprocessor = MHD_create_post_processor(connection, POSTBUFFERSIZE, IteratePost, (void*)con_info); - - if(NULL == con_info->postprocessor){ - - printf("POST MHD_NO\n"); - free(con_info); - return MHD_NO; + if(size != 0){ + for(i = 0; i < size; i++){ + printf("%s\n", parse_result[i]); } - printf("coninfo->postprocessor is not null\n"); - con_info->connectiontype = POST; - }else{ - con_info->connectiontype = GET; - } - - - *con_cls = (void*)con_info; - - return MHD_YES; - }*/ - - - if(0 == strcmp(method, "GET")){ - int size = 0; - char **parse_result = ParseUri(&size, url); - int i = 0; - - if(size != 0) - { - - printf("%d\n", size); - for(i = 0; i < size; i++){ - printf("%s\n", parse_result[i]); - } - - char* result = GetSongInfoFromGetMethod(0, json_root, parse_result, size); - printf("result : %s\n", result); + char* result = GetSongInformation(parse_result, size - 1, parse_result[size - 1]); - FreeParseResult(parse_result, size); + printf("result : %s\n", result); - return SendData(connection, result); + FreeParseResult(parse_result, size); + + if(result){ + return SendData(connection, result); } - } + } + } - if(0 == strcmp(method, "POST")){ - // struct connection_info_struct *con_info = *con_cls; + if(0 == strcmp(method, "POST")){ printf("POST\n"); @@ -182,12 +138,8 @@ static int AnswerToConnection(void *cls, struct MHD_Connection *connection, printf("%s\n", upload_data); return SendData(connection, "SUCCESS"); - }/*else if(con_info->answer_object != NULL){ - return SendData(connection, con_info->answer_object); - }*/ - - //return HD_YES; - } + } + } return SendData(connection, "error ocurred"); @@ -214,53 +166,7 @@ static int SendData(struct MHD_Connection *connection, const char *data){ return ret; } -static int IteratePost(void *coninfo_cls, enum MHD_ValueKind kind, const char *key, - const char *filename, const char *content_type, - const char *transfer_encoding, const char *data, uint64_t off, - size_t size){ - - printf("IteratePost\n"); - - struct connection_info_struct *coninfo = coninfo_cls; - - char *answering = malloc(strlen("hellobrower")); - - answering = "hellobrower"; - - printf("%s\n", data); - -/* json_t *answering = json_object(); - json_t *str = json_string("iteratepost"); - json_object_set(answering, "song", str);*/ - - coninfo->answer_object = answering; - - return MHD_YES; -} - -static void RequestCompleted(void *cls, struct MHD_Connection *connection, - void **con_cls, enum MHD_RequestTerminationCode toe){ - - struct connection_info_struct *coninfo = *con_cls; - - if(coninfo == NULL){ - return; - } - - MHD_destroy_post_processor(coninfo->postprocessor); - - if(coninfo->answer_object){ - free(coninfo->answer_object); - } - - free(coninfo); - *con_cls = NULL; - - printf("RequestCompleted\n"); -} - int InitializeSignal(){ - if(signal(SIGINT, Sigint_Handler) == SIG_ERR){ printf("cannot handle SIGINT\n"); return FALSE; @@ -351,76 +257,137 @@ void FreeParseResult(char **parse_result, int size){ } } -char* GetSongInfoFromGetMethod(int index, json_t* json_data, char **parse_result, int size){ - if(size == 0 || parse_result == NULL){ - return NULL; - } +json_t* FindDataFromJsonObject(json_t *object, char *key_data){ + void *iter = json_object_iter(object); + const char *key = NULL; + json_t* data_object = NULL; - if(size -1 == index){ - size_t size = json_array_size(json_data); - json_t *value = NULL; - int isfound = FALSE; - size_t i = 0; + while(iter){ + key = json_object_iter_key(iter); - for(i = 0; i < size; i++){ - json_t *song_object = json_array_get(json_data, i); - void* iter = json_object_iter(song_object); - const char *key = NULL; + if(0 == strcasecmp(key, key_data)){ + data_object = json_object_iter_value(iter); + break; + } + + iter = json_object_iter_next(object, iter); + } + + return data_object; +} - while(iter){ - key = json_object_iter_key(iter); +json_t* FindDataFromJsonArray(json_t *array, char *key_data){ + size_t size = json_array_size(array); + size_t i = 0; + json_t* data_object = NULL; - if(0 == strcasecmp(key, "song")){ - const char *song_name = json_string_value(json_object_get(song_object, key)); + for(i = 0; i < size; i++){ + json_t *json_data = json_array_get(array, i); - if(0 == strcmp(parse_result[index],song_name)){ - isfound = TRUE; - break; - } - } + if(json_is_object(json_data)){ + data_object = FindDataFromJsonObject(json_data, key_data); + } - iter = json_object_iter_next(song_object, iter); - } + if(!data_object){ + break; + } + } - if(isfound){ - value = song_object; - break; - } - } + return data_object; +} + +int GetLyricFromJsonObject(json_t *object, char *song_name){ + void *iter = NULL; + + json_t *song_string = json_object_get(object, "song"); + + if(!song_string){ + return FALSE; + } - if(isfound){ + const char *song_data = json_string_value(song_string); + + return (0 == strcasecmp(song_data, song_name)); +} - if(0 == strcasecmp(parse_result[index - 1], "lyrics")){ - json_t *lyric = json_object_get(value, "lyric"); - json_t *object = json_object(); +int GetSingerFromJsonObject(json_t *object, char *song_name){ + void *iter = NULL; + json_t *song_array = json_object_get(object, "song"); + size_t size = json_array_size(song_array); + size_t i = 0; - json_object_set(object, "song", lyric); + for(i = 0; i < size; i++){ + json_t *song_string = json_array_get(song_array, i); - char* res = json_dumps(object, JSON_ENCODE_ANY); + if(0 == strcasecmp(song_name, json_string_value(song_string))){ + return TRUE; + } + } + return FALSE; +} - return res; - } +char* GetSongInformation(char **parse_result, int size, char *song_name){ + size_t i = 0; + json_t *temp = json_root; + json_t *new_object = NULL; + + for(i = 0; i < size; i++){ + if(json_is_object(temp)){ + temp = FindDataFromJsonObject(temp, parse_result[i]); + }else if(json_is_array(temp)){ + temp = FindDataFromJsonArray(temp, parse_result[i]); + }else{ + temp = NULL; } - return NULL; + if(!temp){ + return NULL; + } } - void *iter = json_object_iter(json_data); - const char* key = NULL; + if(0 == strcasecmp(parse_result[size - 1], "lyrics")){ + size_t size = json_array_size(temp); + json_t *lyric = NULL; - while(iter){ - key = json_object_iter_key(iter); - - if(0 == strcasecmp(parse_result[index], key)){ - printf("found key : %s parse_result : %s index : %d\n", key, parse_result[index], index); + for(i = 0; i < size; i++){ + json_t *object = json_array_get(temp, i); - return GetSongInfoFromGetMethod(index + 1, json_object_iter_value(iter), parse_result, size); + if(TRUE == GetLyricFromJsonObject(object, song_name)){ + lyric = json_object_get(object, "lyric"); + break; + } + } + + if(lyric){ + new_object = json_object(); + json_object_set(new_object, "lyric", lyric); + } + }else if(0 == strcasecmp(parse_result[size - 1], "singer")){ + size_t size = json_array_size(temp); + json_t *singer = NULL; + + printf("singer\n"); + for(i = 0; i < size; i++){ + json_t *object = json_array_get(temp, i); + + if(TRUE == GetSingerFromJsonObject(object, song_name)){ + singer = json_object_get(object, "name"); + break; + } } - iter = json_object_iter_next(json_data, iter); - } + if(singer){ + new_object = json_object(); + json_object_set(new_object, "singer", singer); + } + }else{ + return NULL; + } - return NULL; + if(!new_object){ + return NULL; + }else{ + return json_dumps(new_object, JSON_ENCODE_ANY); + } } -