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

查看:133
本文介绍了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。
我也重定向每个请求动态目录(无需使用字符?),而$ P $ url中pventing使用扩展名为.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.

您所期待的所有要求是能获得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

我没有测试code以上,因此可能无法正常工作,但至少你会得到关于如何做到这一点的想法。另外,剧本不处理管道内衬的要求。

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天全站免登陆