#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>)]]