2013-02-27 163 views
2

我試圖爲我的腳本實現一個寧靜的處理程序,用&代替/,這樣我可以將url的這樣:?script.c&things轉換爲:?script/things。目前我只有一個測試腳本,它基於Gil的thisGwan處理程序狀態

// ============================================================================ 
    // Handler C script for the G-WAN Web Application Server (http://gwan.ch/) 
    // ---------------------------------------------------------------------------- 
    // main.c: basic rewrite example 
    // ============================================================================ 
    #include "gwan.h" // G-WAN exported functions 

    #include <stdio.h> // puts(), printf() 
    // ---------------------------------------------------------------------------- 
    // init() will initialize your data structures, load your files, etc. 
    // ---------------------------------------------------------------------------- 
    // init() should return -1 if failure (to allocate memory for example) 
    int init(int argc, char *argv[]) 
    { 
    // define which handler states we want to be notified in main(): 
    // enum HANDLER_ACT { 
    // HDL_INIT = 0, 
    // HDL_AFTER_ACCEPT, // just after accept (only client IP address setup) 
    // HDL_AFTER_READ, // each time a read was done until HTTP request OK 
    // HDL_BEFORE_PARSE, // HTTP verb/URI validated but HTTP headers are not 
    // HDL_AFTER_PARSE, // HTTP headers validated, ready to build reply 
    // HDL_BEFORE_WRITE, // after a reply was built, but before it is sent 
    // HDL_HTTP_ERRORS, // when G-WAN is going to reply with an HTTP error 
    // HDL_CLEANUP }; 
    u32 *states = (u32*)get_env(argv, US_HANDLER_STATES); 
    *states = (1 << HDL_AFTER_READ); 
    return 0; 
    } 
    // ---------------------------------------------------------------------------- 
    // clean() will free any allocated memory and possibly log summarized stats 
    // ---------------------------------------------------------------------------- 
    void clean(int argc, char *argv[]) 
    {} 
    // ---------------------------------------------------------------------------- 
    // main() does the job for all the connection states below: 
    // (see 'HTTP_Env' in gwan.h for all the values you can fetch with get_env()) 
    // ---------------------------------------------------------------------------- 
    int main(int argc, char *argv[]) 
    { 
    // HDL_HTTP_ERRORS return values: 
    // 0: Close the client connection 
    // 2: Send a server reply based on a custom reply buffer 
    // 255: Continue (send a reply based on the request HTTP code) 
    const long state = (long)argv[0]; 
    printf("Catching Gwan State: %i\n", (long)argv[0]); 
    if(state != HDL_AFTER_READ) 
     return 255; 

    xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF); 
    printf("req_1: %.20s\n", read_xbuf->ptr); 
    xbuf_replfrto(read_xbuf, read_xbuf->ptr, read_xbuf->ptr + 16, "/", "&"); 
    printf("req_2: %.20s\n-------------------\n\n", read_xbuf->ptr); 

    return 255; // continue G-WAN's default execution path 
    } 
    // ============================================================================ 
    // End of Source Code 
    // ============================================================================ 

在這個腳本我有一個printf("Catching Gwan State: %lu\n", (long)argv[0]);線是應該打印得到的狀態(0-8,我猜),但它一直在打印

Catching Gwan State: -38241808 

我不知道是什麼 - 38241808是

任何幫助?我的操作系統是Linux Mint的14,金桂冠版本4.2.19

[編輯]使用隨金桂冠的main_generic.c處理例如即使給這些奇怪的狀態值

回答

2

我想實現我的腳本寧靜的處理程序,它取代了/有&,所以我可以把網址是這樣的:?script.c&things這個:?script/things

G-WAN自動自動。處理程序是絕對不需要的。 RESTful功能記錄在PDF手冊時間軸上。

甚至可以定義哪種編程語言是默認語言(不需要URI中的顯式文件擴展名)。見怎麼辦呢下面(這裏從處理程序):

int init(int argc, char *argv[]) 
{ 
    // the QUERY_CHAR character can be chosen from the following set: 
    // - _ . ! ~ * ' () 
    // (see RFC 2396, section "2.3. Unreserved Characters") 
    // 
    u8 *query_char = (u8*)get_env(argv, QUERY_CHAR); 
    *query_char = '!'; // use "/!hello.c" instead of "/?hello.c" 

    // by default, DEFAULT_LANG = LG_C (ANSI C) 
    // LG_C, LG_CPP, LG_JAVA, etc. are defined in /gwan/include/gwan.h 
    // and in http://gwan.com/api#env 
    // 
    u8 *lang = (u8*)get_env(argv, DEFAULT_LANG); 
    *lang = LG_CPP; // use "/!hello" instead of "/!hello.cpp" 
    return 0; 
} 

使用/?argv/123/456,而不是與/?argv.c&123&456 G-WAN例子只是測試它...

+0

我覺得自己很傻。我已經閱讀了手冊(很多次),並且我明白,您可以實現一個寧靜的架構,並且您可以更改默認語言,但我沒有意識到它已經到位。非常感謝Gil – 2013-02-28 00:57:29

+0

我仍然想知道爲什麼處理程序獲得奇怪的值供將來參考 – 2013-02-28 01:49:29

+0

我現在試圖使用處理程序在url的末尾添加一個隨機值以避免微緩存。但是我仍然收到argv [0]的奇怪值,我已經更新到gwan v 4.3.1 – 2013-03-05 22:43:20

-1

我測試了我的。 如果你喜歡這款
u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
*states = (1L << HDL_AFTER_ACCEPT)
| (1L << HDL_AFTER_READ)
| (1L << HDL_BEFORE_PARSE)
| (1L << HDL_AFTER_PARSE)
| (1L << HDL_HTTP_ERRORS)
| (1L << HDL_BEFORE_WRITE);

它會產生0-8。

+0

它似乎並沒有幫助 – 2013-02-27 02:25:56

+0

您需要重新啓動gwan – csw 2013-02-27 03:05:42

+0

我每次重新啓動時都會重新啓動處理程序(只是可以肯定),但仍然很奇怪 – 2013-02-27 04:28:50