ragel解析http url

本文介绍了一个基于RFC3986标准的通用URI解析器实现细节。该解析器使用C++编写,通过状态机的方式解析URI,并将各部分信息如scheme、authority、path等分离出来。通过对URI结构的深入分析,该解析器能够有效处理各种复杂的URI格式。
#include <iostream>
#include <string>

using namespace std;

%%{
   
   
    # See RFC 3986: http://www.ietf.org/rfc/rfc3986.txt

    machine uri_parser;

    gen_delims = ":" | "/" | "?" | "#" | "[" | "]" | "@";
    sub_delims = "!" | "$" | "&" | "'" | "(" | ")" | "*" | "+" | "," | ";" | "=";
    reserved = gen_delims | sub_delims;
    unreserved = alpha | digit | "-" | "." | "_" | "~";
    pct_encoded = "%" xdigit xdigit;

    action marku {
   
    mark = fpc; }
    action markh {
   
    mark = fpc; }

    action save_scheme
    {
   
   
        uri->setScheme(std::string(mark, fpc - mark));
        mark = NULL;
    }

    scheme = (alpha (alpha | digit | "+" | "-" | ".")*) >marku %save_scheme;

    action save_port
    {
   
   
        if (fpc != mark) {
   
   
            uri->setPort(atoi(mark));
        }
        mark = NULL;
    }
    action save_userinfo
    {
   
   
        if(mark) {
   
   
            //std::cout << std::string(mark, fpc - mark) << std::endl;
            uri->setUserinfo(std::string(mark, fpc - mark));
        }
        mark = NULL;
    }
    action save_host
    {
   
   
        if (mark != NULL) {
   
   
            //std::cout << std::string(mark, fpc - mark) << std::endl;
            uri->setHost(std::string(mark
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值