Microsoft MVP성태의 닷넷 이야기
VC++: 48. 윈도우에서 Apache Module - Content Handler 컴파일 [링크 복사], [링크+제목 복사],
조회: 28175
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 3개 있습니다.)
윈도우에서 Apache Module - Content Handler 컴파일 (VC++)

지난번에 만들어 본 모듈은 다음과 같이 httpd.conf에 SetHandler로 설정되는 유형이었습니다.

<Location /example-info>
    SetHandler example-handler
</Location>

그런데, 여기서 궁금함이 생겼습니다. 예전에 알아본 PHP의 경우에는 다음과 같이 추가를 AddType으로 추가를 했었는데, 그럼 그 둘 간에는 어떤 차이가 있냐는 것인데요.

AddType application/x-httpd-php .php

검색해 보니, PHP와 같은 경우를 별도로 "Content Handler"라고 부르는 것 같습니다. 궁금하니 어쩔 수 없습니다, 한번 만들어 봐야지. ^^ 이를 위해 검색해 보니 다음의 글이 눈에 띕니다.

2. Apache Content Handler
; http://woorie.net/entry/2-Apache-Content-Handler

위의 글에 Content Handler에 대한 소스 코드가 실려 있는데, 기존 모듈의 소스 코드와 차이점이 없어 보입니다. 안 좋은 소식이라면, 이 예제는 아파치 2.0 미만의 버전과 호환이 되는 것 같았습니다. 어쨌든 중요한 것은 뭔가 크게 다르지 않다는 것! ^^

#include "httpd.h" 
#include "http_config.h" 
#include "http_protocol.h" 
#include "ap_config.h" 

/* The sample content handler */ 
static int turbo08_handler(request_rec *r) 
{ 
    r->content_type = "text/html"; 
    ap_send_http_header(r); 

    if (!r->header_only) 
        ap_rputs("The sample page from mod_turbo08.c\n", r); 

    return OK; 
} 

/* Dispatch list of content handlers */ 
static const handler_rec turbo08_handlers[] = 
    { 
        { "turbo08", turbo08_handler }, 
        { NULL, NULL } 
    }; 

/* Dispatch list for API hooks */ 
module MODULE_VAR_EXPORT turbo08_module = 
    { 
        STANDARD_MODULE_STUFF, 
        NULL, /* module initializer */ 
        NULL, /* create per-dir config structures */ 
        NULL, /* merge per-dir config structures */ 
        NULL, /* create per-server config structures */ 
        NULL, /* merge per-server config structures */ 
        NULL, /* table of config file commands */ 
        turbo08_handlers, /* [#8] MIME-typed-dispatched handlers */ 
        NULL, /* [#1] URI to filename translation */ 
        NULL, /* [#4] validate user id from request */ 
        NULL, /* [#5] check if the user is ok _here_ */ 
        NULL, /* [#3] check access by host address */ 
        NULL, /* [#6] determine MIME type */ 
        NULL, /* [#7] pre-run fixups */ 
        NULL, /* [#9] log a transaction */ 
        NULL, /* [#2] header parser */ 
        NULL, /* child_init */ 
        NULL, /* child_exit */ 
        NULL /* [#0] post read-request */ 
    };

이해도를 높이기 위해 직접 제작을 해볼텐데요. ^^ 예전 글에서는 아파치에서 기본 제공되는 experimental 예제를 복사해서 했지만, 이번에는 Visual Studio 2008을 이용해서 바닥부터 프로젝트를 만들어 컴파일해 보겠습니다.

우선, Win32 Project를 생성하고 (제 경우에는 프로젝트 이름을 MyContentHandler로 명명) DLL 응용 프로그램 유형으로 시작했습니다.

how_to_build_apache_content_handler_1.png

물론, 프로젝트 속성을 몇 가지 바꿔주어야 합니다.

"General" / "Character Set"
==> Not Set

"Debugging" / "Command"
==> D:\httpd_build\Apache22\bin\httpd.exe

"C/C++" / "Additional Include Directories" - 아파치 헤더 파일 경로 설정
==> ../../include,../../srclib/apr/include,../../srclib/apr-util/include

"Linker" / "Output File" - 디버그하기 편리하도록 아파치 모듈 폴더에 곧바로 출력
==> ..\..\..\Apache22\modules\mycontenthandler_module.so

그다음, 기본 생성된 파일을 변경합니다.

  • stdafx.h 파일의 "#include windows.h" 라인을 주석 처리
  • dllmain.cpp 파일 제거

생성되어 있던 MyContentHandler.h 파일에 다음과 같이 헤더 파일 및 export 심벌 선언을 추가해 주고,

#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"
#include "http_connection.h"

#include "apr_strings.h"

#include 

#pragma comment(lib, "../lib/libhttpd.lib")
#pragma comment(lib, "../lib/libapr-1.lib")
#pragma comment(lib, "../lib/libaprutil-1.lib")

extern "C"
{
    extern module AP_MODULE_DECLARE_DATA mycontenthandler_module;
}

MyContentHandler.cpp 파일에는 다음과 같이 코드를 추가해줍니다.

#include "stdafx.h"
#include "MyContentHandler.h"

static void x_register_hooks(apr_pool_t *p)
{
}

module AP_MODULE_DECLARE_DATA mycontenthandler_module = 
{ 
    STANDARD20_MODULE_STUFF, 
    NULL, /* create per-dir config structures */ 
    NULL, /* merge per-dir config structures */ 
    NULL, /* create per-server config structures */ 
    NULL, /* merge per-server config structures */ 
    NULL, /* table of config file commands */ 
    x_register_hooks, /* dispatched handlers */ 
};

여기까지 일단 빌드를 하고, depends.exe로 결과를 확인해 봅니다. 반드시 아래와 같이 mycontenthandler_module이 export 되어 있어야 합니다.

how_to_build_apache_content_handler_2.png




돌아가는 분위기를 보아하니, Content Handler라고 해서 특별한 점은 없어 보입니다. 즉, 지난번에 알아본 핸들러와도 소스 코드 구조 면에서 완전히 일치합니다. 단지, httpd.conf 파일에 어떻게 등록하느냐에 따라서 그것이 Content Handler라고 불리는 것 같습니다. 어디... 확인을 해봐야겠지요. ^^ 이를 위해 간단한 x_handler 함수를 만들어 두고 x_register_hooks에서 등록시켜 줍니다.

static int x_handler(request_rec *r)
{
    return OK;
}

static void x_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

남은 작업은 [아파치 설치 폴더]/conf/httpd.conf 파일에 다음과 같이 Content Handler를 등록해 주는 것입니다.

LoadModule mycontenthandler_module modules/mycontenthandler_module.so

AddType mycontenthandler_module myext

위와 같이 해주고, (Shift + F2 디버깅 상태에서) 웹 브라우저로 "http://localhost:65000/test.txt"라고 방문했더니 제가 예상했던 것과는 다르게, 원하는 확장자(myext)가 아님(txt)에도 불구하고 mycontenthandler_module.so 모듈에 정의된 x_handler가 호출되는 것이 확인되었습니다.

비효율적이라고 생각되었지만, 어쨌든 이제 다시 한번 궁금한 점이 생깁니다. 도대체 그럼, x_handler에서는 test.txt로 요청이 들어왔는지, test.myext로 요청이 들어왔는지 어떻게 구분하고 반응을 해야 하는 걸까요?

아파치는 AddType 정의가 추가된 경우, 그곳에 등록된 확장자(위의 예에서는 myext)로 요청이 들어오면 AddType의 첫 번째 인자(위의 예에서는 mycontenthandler)로 주어진 문자열을 x_handler 메서드의 첫 번째 파라미터인 request_rec의 handler 멤버에 설정을 해줍니다.

아래는, 제가 "Shift + F2" 디버깅상에서 확인한 결과입니다.

[그림 1: test.txt로 방문한 경우, r->handler == "text/plain"]
how_to_build_apache_content_handler_3.png

[그림 2: test.myext로 방문한 경우, r->handler == "mycontenthandler_module"]
how_to_build_apache_content_handler_4.png

따라서, 자신이 담당해야 할 확장자로 들어온 요청은 AddType에 지정된 첫 번째 문자열 값이 r->handler에 들어왔을 때를 기준으로 구분을 하면 되므로, 다음과 같이 코드를 작성해 줄 수 있습니다.

static int x_handler(request_rec *r)
{
    if (strcmp(r->handler, "mycontenthandler_module"))
    {
        return DECLINED;
    }

    ap_set_content_type(r, "text/html");

    if (r->header_only) 
    {
        return OK;
    }

    ap_rputs("test is good!", r); 

    return OK;
}

이쯤에서 다시 PHP 모듈 등록 방식으로 돌아가 볼까요?

AddType application/x-httpd-php .php

아하~~~ 이제야 눈에 들어오는 군요. 아파치는 .php 확장자로 요청이 들어온 경우에는 "LoadModule"로 등록된 모든 모듈의 x_handler를 호출하면서 r->handler에 "application/x-httpd-php" 문자열을 전달해 줄 것이고, 그중에서 php5apache2_2.dll 안에 정의된 x_handler 동격의 함수에서 다음과 같은 처리 코드를 가지고 시작할 거라고 예상해 볼 수 있습니다.

static int x_handler(request_rec *r)
{
    if (strcmp(r->handler, "application/x-httpd-php"))
    {
        return DECLINED;
    }

... [생략] ...
    return OK;
}




마지막으로 정리해 보면, 결국 일반 모듈이나 Content Handler라고 불리는 모듈이나 제작 방식은 동일하고, 단순히 httpd.conf 파일에 SetHandler로 등록하느냐, AddType으로 등록하느냐에 대한 차이만 있는 것입니다. (아마도, 다른 AddEncoding/AddHandler 등의 모듈도 제작 방식은 동일할 것 같습니다.)

저 같은 아파치 초보자는 여기까지 개념을 잡는 것도 오래 걸리는 군요. ^^

(첨부한 파일은 위의 예제 코드를 담고 있는 VC++ 프로젝트입니다.)



[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/28/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13718정성태8/27/20247440오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
13717정성태8/26/20247034VS.NET IDE: 192. Visual Studio 2022 - Windows XP / 2003용 C/C++ 프로젝트 빌드
13716정성태8/21/20246767C/C++: 167. Visual C++ - 윈도우 환경에서 _execv 동작 [1]
13715정성태8/19/20247384Linux: 78. 리눅스 C/C++ - 특정 버전의 glibc 빌드 (docker-glibc-builder)
13714정성태8/19/20246761닷넷: 2295. C# 12 - 기본 생성자(Primary constructors) (책 오타 수정) [3]
13713정성태8/16/20247493개발 환경 구성: 721. WSL 2에서의 Hyper-V Socket 연동
13712정성태8/14/20247228개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/20248076Linux: 77. C# / Linux - zombie process (defunct process) [1]파일 다운로드1
13710정성태8/8/20248006닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/20247766닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/20247557개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/20247792닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/20247899개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/20248170닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용 [2]파일 다운로드1
13704정성태8/2/20248126닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/20247451닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/20247861닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/20248130닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/20247756디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/20248251닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/20248213닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
13697정성태7/26/20247933닷넷: 2284. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/20247469오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
13695정성태7/25/20247455닷넷: 2283. C# - Lock / Wait 상태에서도 STA COM 메서드 호출 처리파일 다운로드1
13694정성태7/25/20247920닷넷: 2282. C# - ASP.NET Core Web App의 Request 용량 상한값 (Kestrel, IIS)
13693정성태7/24/20247245개발 환경 구성: 717. Visual Studio - C# 프로젝트에서 레지스트리에 등록하지 않은 COM 개체 참조 및 사용 방법파일 다운로드1
1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...