Skip to content
Snippets Groups Projects
Commit ecb04ea0 authored by 권 기쁨's avatar 권 기쁨
Browse files

first http

parents
No related branches found
No related tags found
No related merge requests found
/* Feel free to use this example code in any way
you see fit (Public Domain) */
#include <sys/types.h>
#ifndef _WIN32
#include <sys/select.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <microhttpd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(_MSC_VER) && _MSC_VER+0 <= 1800
/* Substitution is OK while return value is not used */
#define snprintf _snprintf
#endif
#define PORT 8888
#define POSTBUFFERSIZE 512
#define MAXNAMESIZE 20
#define MAXANSWERSIZE 512
#define GET 0
#define POST 1
struct connection_info_struct
{
int connectiontype;
char *answerstring;
struct MHD_PostProcessor *postprocessor;
};
const char *error_message =
"{ \"Messge \" : \"Error\" }\n";
const char *havana_lyrics =
"{\n\"lyrics \" :\n\
\"Havana ooh na-na ay\n\
Half of my heart is in Havana ooh-na-na ay ay\n\
He took me back to East Atlanta na-na-na\n\
All of my heart is in Havana ay\n\
There\'s somethin\' \'bout his manners uh huh\n\
Havana ooh na-na\n}\n";
const char *havana_singer =
"{ \"singer \" : \"Camila Cabello\" }\n";
// 응답 전송
static int send_page (struct MHD_Connection *connection, const char *page){
int ret;
struct MHD_Response *response;
response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT);
if (!response)
return MHD_NO;
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
return ret;
}
// 노래가사 요청 시
static int lyrics_post (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)
{
struct connection_info_struct *con_info = coninfo_cls;
// 노래제목 조회한다
if (0 == strcmp (key, "song")){
// 노래제목이 havana
if(0 == strcmp (data, "havana")){
if ((size > 0) && (size <= MAXNAMESIZE)) {
char *answerstring;
answerstring = malloc (MAXANSWERSIZE);
if (!answerstring)
return MHD_NO;
snprintf (answerstring, MAXANSWERSIZE, havana_lyrics, data);
con_info->answerstring = answerstring;
printf("{\n \"%s\" : \"%s\"\n}\n", key, data);
}
else
con_info->answerstring = NULL;
}
return MHD_NO;
}
return MHD_YES;
}
// 가수이름 요청 시
static int singer_post (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)
{
struct connection_info_struct *con_info = coninfo_cls;
// 노래제목 조회한다
if (0 == strcmp (key, "song")){
// 노래 제목 havana
if(0 == strcmp (data, "havana")){
if ((size > 0) && (size <= MAXNAMESIZE)) {
char *answerstring;
answerstring = malloc (MAXANSWERSIZE);
if (!answerstring)
return MHD_NO;
snprintf (answerstring, MAXANSWERSIZE, havana_singer, data);
con_info->answerstring = answerstring;
printf("{\n \"%s\" : \"%s\"\n}\n", key, data);
}
else
con_info->answerstring = NULL;
}
return MHD_NO;
}
return MHD_YES;
}
static void request_completed (void *cls, struct MHD_Connection *connection,
void **con_cls, enum MHD_RequestTerminationCode toe)
{
struct connection_info_struct *con_info = *con_cls;
if (NULL == con_info)
return;
if (con_info->connectiontype == POST)
{
MHD_destroy_post_processor (con_info->postprocessor);
if (con_info->answerstring)
free (con_info->answerstring);
}
free (con_info);
*con_cls = NULL;
}
char get_key[10] = {0, };
char get_value[10] = {0, };
// MHD_get_connection_values 함수에서 인자로 사용
// GET 전송 시 key-value 출력해주는 함수
int print_out_key(void *cls, enum MHD_ValueKind kind, const char *key, const char *value){
printf("{\n \"%s\" : \"%s\"\n}\n", key, value);
snprintf (get_key, strlen(key) + 1, key);
snprintf (get_value, strlen(value) + 1, value);
return MHD_YES;
}
// GET or POST 구분하여 send_page 호출
// POST 일 경우 POST 프로세서 생성
static int answer_to_connection (void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
if (NULL == *con_cls) {
struct connection_info_struct *con_info;
con_info = malloc (sizeof (struct connection_info_struct));
if (NULL == con_info)
return MHD_NO;
con_info->answerstring = NULL;
if (0 == strcmp (method, "POST")) {
// 가사 조회
if (0 == strcmp (url, "/info/lyrics")){
con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE, lyrics_post, (void *) con_info);
}
// 가수 조회
else if (0 == strcmp (url, "/info/singer")){
con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE, singer_post, (void *) con_info);
}
// 없는 api 조회
else {
return send_page (connection, error_message);
}
if (NULL == con_info->postprocessor) {
free (con_info);
printf("con_info deallocate\n");
return MHD_NO;
}
con_info->connectiontype = POST;
}
else if (0 == strcmp (method, "GET")){
struct MHD_Response *response;
int result;
// 2번째 인자 enum MHD_ValueKind : key-value pairs
// MHD_GET_ARGUMENT_KIND : GET uri argument
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, &print_out_key, NULL);
// 가사 조회
if (0 == strcmp (url, "/info/lyrics")){
if((0 == strcmp(get_key, "song")) && (0 == strcmp(get_value, "havana"))){
response = MHD_create_response_from_buffer(strlen(havana_lyrics), (void *)havana_lyrics, MHD_RESPMEM_PERSISTENT);
}
else {
return send_page (connection, error_message);
}
}
// 가수 조회
else if (0 == strcmp (url, "/info/singer")){
if((0 == strcmp(get_key, "song")) && 0 == (strcmp(get_value, "havana"))){
response = MHD_create_response_from_buffer(strlen(havana_singer), (void *)havana_singer, MHD_RESPMEM_PERSISTENT);
}
else {
return send_page (connection, error_message);
}
}
// 없는 api 조회
else {
return send_page (connection, error_message);
}
// 클라이언트에 전송할 응답큐에 넣는다.
// 클라이언트와 연결된 식별자로 상태코드와 응답 전송
result = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
con_info->connectiontype = GET;
}
*con_cls = (void *) con_info;
return MHD_YES;
}
if (0 == strcmp (method, "POST"))
{
struct connection_info_struct *con_info = *con_cls;
if (*upload_data_size != 0) {
MHD_post_process (con_info->postprocessor, upload_data, *upload_data_size);
*upload_data_size = 0;
return MHD_YES;
}
else if (NULL != con_info->answerstring)
return send_page (connection, con_info->answerstring);
}
return send_page (connection, error_message);
}
int main ()
{
struct MHD_Daemon *daemon;
daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, &answer_to_connection, NULL,
MHD_OPTION_NOTIFY_COMPLETED, request_completed,
NULL, MHD_OPTION_END);
if (NULL == daemon)
return 1;
(void) getchar ();
MHD_stop_daemon (daemon);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment