Skip to content

Commit

Permalink
add codes
Browse files Browse the repository at this point in the history
  • Loading branch information
akzi committed Nov 15, 2016
0 parents commit 50d7ff8
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 0 deletions.
144 changes: 144 additions & 0 deletions include/xhttper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#pragma once
#include <map>
namespace xhttper
{

struct parse_error : std::exception {};

class xhttper
{
public:
struct str_ref
{
std::size_t pos_;
std::size_t len_;
};
enum class method
{
e_null,
e_get,
e_post,
e_put,
e_head,
e_delete,
e_connect,
e_trace,
e_options,
};
enum version
{
e_null,
e_1_0,
e_1_1
};
xhttper()
{
buf_.reserve(1024);
}
bool parse(const char *dat, std::size_t len)
{
buf_.append(dat, len);
return do_parser();
}
private:
/*
GET /download.google.com/somedata.exe HTTP/1.1
Host: download.google.com
Accept:
Pragma : no - cache
Cache-Control : no - cache
Referer : http://download.google.com/
User-Agent : Mozilla / 4.04[en](Win95; I; Nav)
Range : bytes = 554554 -
*/
bool do_parser()
{
if (!get_method())
return false;
if (!get_path())
return false;
}
bool get_path()
{
if (!skip_space())
return false;

}
bool skip_space()
{
while (pos_ < buf_.size() )
{
char ch = buf_[pos_];
if (ch == ' ' ||
ch == '\t' ||
ch == '\r' ||
ch == '\r')
pos_++;
else
return true;
}
return false;
}
bool get_method()
{
if (method_ == method::e_null)
{
set_rollback();
auto text = get_str(' ');
if (text == "POST")
method_ = method::e_post;
else if (text == "GET")
method_ = method::e_get;
else if (text == "PUT")
method_ = method::e_put;
else if (text == "HEAD")
method_ = method::e_head;
else if (text == "CONNECT")
method_ = method::e_connect;
else if (text == "TRACE")
method_ = method::e_trace;
else if (text == "OPTIONS")
method_ = method::e_options;

if (method_ == method::e_null)
{
if (pos_ == buf_.size())
{
rollback();
return false;
}
else
throw parse_error();
}
}
return true;
}
std::string get_str(char end)
{
std::string result;
while (pos_ < buf_.size() && buf_[pos_] != end)
{
result.push_back(end);
}
return std::move(result);
}
void set_rollback()
{
last_pos_ = pos_;
}
void rollback()
{
pos_ = last_pos_;
}


method method_ = method::e_null;
str_ref path_;
version version_ = e_null;

std::size_t last_pos_ = 0;
std::size_t pos_= 0;
std::string buf_;
std::map<str_ref, str_ref> headers_;
};
}
28 changes: 28 additions & 0 deletions xhttper.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xhttper", "xhttper.vcxproj", "{3722C585-49B3-47FB-A3DB-8444AC80BDA5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Debug|x64.ActiveCfg = Debug|x64
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Debug|x64.Build.0 = Debug|x64
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Debug|x86.ActiveCfg = Debug|Win32
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Debug|x86.Build.0 = Debug|Win32
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Release|x64.ActiveCfg = Release|x64
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Release|x64.Build.0 = Release|x64
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Release|x86.ActiveCfg = Release|Win32
{3722C585-49B3-47FB-A3DB-8444AC80BDA5}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
118 changes: 118 additions & 0 deletions xhttper.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\xhttper.hpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{3722C585-49B3-47FB-A3DB-8444AC80BDA5}</ProjectGuid>
<RootNamespace>xhttper</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

0 comments on commit 50d7ff8

Please sign in to comment.