#action Blog 블로그 더하기
##Blog
{{{#!blog hyacinth 2014-07-13T14:55:01 오늘의 수확(스크랩)
신문을 보면 하루에 한 두 개 꼭지 인상적인 글이 있다. 그래서 몇 년 전에는 가위로 잘라서 스크랩하기도 했는데, 지금은 스마트폰으로 사진을 찍으면 아주 정리하기 편하다. 
다음은 어제(7월 12일 토요일) 자 신문과 몇몇 잡지를 보며 얻은 수확물이다.

1. '30년 안에 없어질 직업'
https://lh6.googleusercontent.com/-a8iYmCJX2L8/U8Jw0hnw9VI/AAAAAAAAEuA/7NrCmZc8dsU/w1313-h985-no/20140713_172132.jpg
지난 3월 17일 LA타임스는 지진 관련 소식을 전하는 속보 기사를 온라인에 냈다.
(중략)
로봇이 쓴 속보 기사에 BBC 등 전 세계 언론은 민감하게 반응했다. 

2. 코세라
https://lh4.googleusercontent.com/-wL6m4N65AeY/U8JwwXNmMyI/AAAAAAAAEvY/1pNFnCwbjHI/w739-h985-no/20140713_170344.jpg

3. "지금 왜 이걸 하고 있지" … 매일 스스로 질문 던져라
https://lh6.googleusercontent.com/-4d6NMuhUs4Y/U8Jw9lsObiI/AAAAAAAAEuY/DOaAsCJR-Vg/w1313-h985-no/20140713_181248.jpg

4. 사설
https://lh3.googleusercontent.com/-0ug4Fz6czXo/U8JxK27e8BI/AAAAAAAAEu8/zH-uv15XYRI/w1313-h985-no/20140713_182115.jpg
}}}

[[HTML(<center>)]]
http://hyacinth.byus.net/img/flower.jpg
[[HTML(</center>)]]

{{{#!blog hyacinth 2014-07-04T05:43:25 경량 C++ 웹 서버 소개 civetweb
{{{+1 글의 목적 \}}}
최근 C/C++로 구현된 간단한 웹 서버를 찾게 되었는데 적절한 프로젝트를 찾아서 소개한다.


{{{+1 소개 \}}}

GitHub:bel2125/civetweb

Civetweb. C/C++로 구현된 웹 서버. 프로젝트 저자는 임베디드 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>)]]