#action Blog 블로그 ë”하기 ##Blog {{{#!blog hyacinth 2014-07-04T05:43:25 경량 C++ 웹 서버 소개 civetweb {{{+1 ê¸€ì˜ ëª©ì \}}} 최근 C/C++ë¡œ êµ¬í˜„ëœ ê°„ë‹¨í•œ 웹 서버를 찾게 ë˜ì—ˆëŠ”ë° ì ì ˆí•œ 프로ì 트를 찾아서 소개한다. {{{+1 소개 \}}} GitHub:bel2125/civetweb C/C++ë¡œ êµ¬í˜„ëœ ì›¹ 서버 Civetwebì„ ì†Œê°œí•œë‹¤. 프로ì 트 ì €ìžëŠ” ìž„ë² ë””ë“œ C++ 웹 서버ë¼ê³ í‘œí˜„í•˜ê³ ìžˆë‹¤. 사용하기 ì‰½ê³ ê°•ë ¥í•˜ë‹¤. HTTP와 CGI, SSL, SSI, Lua 서버 페ì´ì§€ ë“±ì„ ì§€ì›í•œë‹¤. Civetwebì€ MIT ë¼ì´ì„ 스로 ìƒìš© ì†Œí”„íŠ¸ì›¨ì–´ë„ ì œí•œì—†ì´ ì‚¬ìš©í• ìˆ˜ 있다. Civetwebì€ Cë¡œ êµ¬í˜„ëœ ì›¹ 서버 mongoose 프로ì 트([https://code.google.com/p/mongoose/])ì—ì„œ 분리ë˜ì—ˆë‹¤. mongoose는 ì´ì „까지 MIT ë¼ì´ì„ 스였으나 2013ë…„ 8ì›” GNU GPL v2 ë¼ì´ì„ 스로 변경ë˜ì—ˆë‹¤. 즉, Civetwebì€ mongooseê°€ GPL v2 ë¼ì´ì„ 스를 가지게 ë¨ì— ë”°ë¼ mongooseê°€ ì•„ì§ MIT ë¼ì´ì„ ìŠ¤ì¼ ë•Œ 분리ë˜ì–´ MIT ë¼ì´ì„ 스를 ìœ ì§€í•˜ëŠ” ê²ƒì´ Civetwebì´ ëª…ì‹œí•˜ê³ ìžˆëŠ” 프로ì íŠ¸ì˜ ê°€ìž¥ í° ëª©ì ì´ë‹¤. (프로ì íŠ¸ì— ê¸°ì—¬í•˜ëŠ” 개발ìžë“¤ì—ê²Œë„ GPL ë¼ì´ì„ 스 변경 ì´í›„ mongoose 코드를 사용하지 ë§ ê²ƒì„ ê°•ì¡°í•˜ê³ ìžˆë‹¤. ìµœì‹ ê¸°ëŠ¥ê³¼ Sergey Lyubka(mongooseì˜ ì €ìž)ê°€ ë§Œë“ í›Œë¥í•œ ê¸°ëŠ¥ë“¤ì„ ì‚¬ìš©í•˜ê³ ì‹¶ìœ¼ë©´ mongoose를 사용해 달ë¼ê³ ë§í•˜ê³ 있다. ë¬¼ë¡ GNU v2 ë¼ì´ì„ 스를 ìœ ì§€í• ìˆ˜ 있다면.) ë”°ë¼ì„œ GPL ë¼ì´ì„ 스 ì œì•½ì—ì„œ ìžìœ ë¡ê³ 싶으면 mongoose ëŒ€ì‹ Civetwebì€ ê°•ë ¥í•œ ëŒ€ì•ˆì´ ë 것ì´ë‹¤. 최종 사용ìžë¥¼ 위한 서버 í”„ë¡œê·¸ëž¨ì€ SourceForgeì—ì„œ 다운로드 ë°›ì„ ìˆ˜ 있다. https://sourceforge.net/projects/civetweb/ 개발ìžë¥¼ 위한 í’€ 소스 ì ‘ê·¼ì´ë‚˜ 기여를 GitHubì—ì„œ í• ìˆ˜ 있다. https://github.com/bel2125/civetweb í† ë¡ ì€ êµ¬ê¸€ ê·¸ë£¹ì— ì—´ë ¤ìžˆë‹¤. https://groups.google.com/d/forum/civetweb {{{+1 Quick Start }}} 다ìŒì€ ê°œë°œìž ìž…ìž¥ì—ì„œ 얼마나 사용하기 간편한지 보여주는 ì˜ˆì œë‹¤. C 프로ì 트ì—ì„œ Civetwebì„ ì‚¬ìš©í•˜ì—¬ HTTP 서버를 ì—´ì–´ Hello civetwebì„ ë¦¬í„´í•˜ëŠ” ì˜ˆì œë‹¤. GitHub:bel2125/civetweb/tree/master/examples/hello {{{#!gcode #include <stdio.h> #include <string.h> #include "civetweb.h" // This function will be called by civetweb on every new request. static int begin_request_handler(struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); char content[100]; // Prepare the message we're going to send int content_length = snprintf(content, sizeof(content), "Hello from civetweb! Remote port: %d", request_info->remote_port); // Send HTTP reply to the client mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: %d\r\n" // Always set Content-Length "\r\n" "%s", content_length, content); // Returning non-zero tells civetweb that our function has replied to // the client, and civetweb should not send client any more data. return 1; } int main(void) { struct mg_context *ctx; struct mg_callbacks callbacks; // List of options. Last element must be NULL. const char *options[] = {"listening_ports", "8080", NULL}; // Prepare callbacks structure. We have only one callback, the rest are NULL. memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; // Start the web server. ctx = mg_start(&callbacks, NULL, options); // Wait until user hits "enter". Server is running in separate thread. // Navigating to http://localhost:8080 will invoke begin_request_handler(). getchar(); // Stop the server. mg_stop(ctx); return 0; } \}}} C 코드는 mongooseê³¼ ê±°ì˜ ê°™ë‹¤. C++ í´ëž˜ìŠ¤ë¡œ ëž©í•‘ëœ ë²„ì „ë„ ìžˆë‹¤. {{{ CivetSevrer.cpp civetweb.c Civetserver.h civetweb.h \}}} MFCì—ì„œ ì‚¬ìš©í•˜ë ¤ë©´ ê° í—¤ë”ì— stdafx.h를 ì¶”ê°€í•˜ê³ civetweb.c 확장ìžë¥¼ .cppë¡œ ë³€ê²½í•˜ê³ ì‚¬ì†Œí•œ ìºìŠ¤íŒ… 오류가 나는 몇 ë¶€ë¶„ì„ ìˆ˜ì •í•˜ë©´ ëœë‹¤. CivetServer í´ëž˜ìŠ¤ëŠ” ë‹¤ìŒ ê°™ì´ ì‚¬ìš©í•œë‹¤. 핸들러 í´ëž˜ìŠ¤ë¥¼ ë§Œë“¤ê³ CivetServer í´ëž˜ìŠ¤ë¥¼ ì„ ì–¸í•´ 핸들러를 등ë¡í•˜ë©´ ë난다. GitHub:bel2125/civetweb/tree/master/examples/embedded_cpp/embedded_cpp.cpp CGI, Lua, WebSocket ë“±ë„ ì˜ˆì œë¥¼ ë³´ë©´ ì–´ë µì§€ 않다. Have fun! }}} [[HTML(<center>)]] http://hyacinth.byus.net/img/flower.jpg [[HTML(</center>)]]