G-WAN 处理程序重写解决方案 [英] G-WAN handler rewriting solution

查看:16
本文介绍了G-WAN 处理程序重写解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本:

#include "gwan.h" // G-WAN exported functions
#include <string.h> // strstr()

int init(int argc, char *argv[])
{
    u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
    *states = 1 << HDL_AFTER_READ;
    return 0;
}

void clean(int argc, char *argv[])
{}

int main(int argc, char *argv[])
{
    if((long)argv[0] == HDL_AFTER_READ)
    {
        xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF);
        if(strstr(read_xbuf->ptr, "GET / HTTP/1.1"))
        {
            xbuf_repl(read_xbuf, "GET / HTTP/1.1", "GET /?index HTTP/1.1");
        }
        else
        {
            if(strstr(read_xbuf->ptr, ".c HTTP/1.1"))
            {
                int *pHTTP_status = (int*)get_env(argv, HTTP_CODE);
                if(pHTTP_status)
                    *pHTTP_status = 404;
                return 255;
            }
            xbuf_repl(read_xbuf, "GET /", "GET /?");
        }
    }
    return(255);
}

如您所知,我正在尝试将主页重定向到动态文件hello.c".我还将每个请求重定向到动态目录(无需使用字符?"),同时防止在 url 中使用扩展名.c".

As you may understood, I'm trying to redirect the homepage to the dynamic file "hello.c". I'm also redirecting every request to the dynamic directory (without having to use the character "?") while preventing the use of the extension ".c" in the url.

此脚本部分工作,但显然会导致内存分配问题.您有什么解决方案可以提出吗?

This script works partly but obviously causes memory allocation issues. Would you have any solution to propose?

推荐答案

如果您担心性能,请不要使用 strstr.它将搜索整个匹配请求.

If you are worried about performance don't use strstr. It will search the whole request for a match.

根据您的脚本,您希望所有请求都是 GET,因此最好使用 strncmp,因为您只比较前 6 个字符.

Based on your script you are expecting all request to be GET so strncmp is better to use since you are only comparing the first 6 characters.

int main(int argc, char *argv[])
{
    xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF);
    if(strncmp(read_xbuf->ptr, "GET / ", 6) == 0)
    {
        xbuf_repl(read_xbuf, " / ", " /?index ");
    }
    else
    {
        int pos = 5; // Start checking after '/' in "GET /"
        while(pos < 20) // Only check first 15 characters
        {               // Adjust depend on longest servlet name
            if(read_xbuf->ptr[pos] == '.' && read_xbuf->ptr[pos+1] == 'c')  // If request contains '.' return 404
            {
                int *pHTTP_status = (int*)get_env(argv, HTTP_CODE);
                if(pHTTP_status)
                    *pHTTP_status = 404;
                return 255;
            }
        }
        xbuf_repl(read_xbuf, "GET /", "GET /?");
    }
    return(255);
}

再次检查.c".您只想检查前 N 个字符.

Again checking for ".c". You only want to check the first N character.

如果您担心添加'?'会导致内存分配对于您需要设计 servlet 名称的每个请求,以便就地替换可以发生.这是一个链接,其中包含有关如何实现就地替换以提高性能的示例.

If you are worried about memory allocation caused by adding '?' to every request you need to design your servlet name so in-place replace can happen. Here is a link that have samples on how to achieve in-place replace for better performance.

G-WAN 中的 RESTful URI

我还没有测试过上面的代码,所以它可能不起作用,但至少你会知道如何去做.此外,该脚本不处理管道请求.

I haven't tested the code above so it might not work but at least you will get an idea on how to do it. Also the script doesn't handle pipe-lined request.

这篇关于G-WAN 处理程序重写解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆