2015-11-23 04:35:40 +08:00
# pragma once
2016-03-14 04:51:04 +08:00
# include "io/network/server.hpp"
2015-11-23 04:35:40 +08:00
# include "debug/log.hpp"
2016-03-14 04:51:04 +08:00
# include "connection.hpp"
2015-11-23 04:35:40 +08:00
namespace http
{
2016-03-14 04:51:04 +08:00
/* const char* body = "Now that the internet can be accessed on any mobile device, whether a laptop, desktop, tablets, smartphone, websites are designed in responsive web version. It is the ability to change the page and font size according to the screen size of the user. desktop, tablets, smartphone, websites are designed in responsive web version. It is the ability to change the page and font size according to the screen size of the user. Thus a website is accessible anytime on any instrument. CSS3 frameworks were widely accepted in 2014 and is growing in 2015. It reduces time and money by helping not creating different sites for different users"; */
2015-11-23 04:35:40 +08:00
2016-03-14 04:51:04 +08:00
/* const char* body = ""; */
2015-11-23 04:35:40 +08:00
2016-03-14 04:51:04 +08:00
std : : string response = " HTTP/1.1 200 OK \r \n Content-Length:0 \r \n \r \n " ;
template < class Req , class Res >
class Parser : public io : : Server < Parser < Req , Res > , Connection < Req , Res > >
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
using Connection = Connection < Req , Res > ;
using Buffer = typename io : : StreamReader < Parser < Req , Res > , Connection > : : Buffer ;
2015-11-23 04:35:40 +08:00
public :
char buf [ 65536 ] ;
2016-03-14 04:51:04 +08:00
Parser ( ) = default ;
2015-11-23 04:35:40 +08:00
2016-03-14 04:51:04 +08:00
Connection & on_connect ( io : : Socket & & socket )
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
auto stream = new Connection ( std : : move ( socket ) ) ;
2015-11-23 04:35:40 +08:00
LOG_DEBUG ( " on_connect socket " < < stream - > id ( ) ) ;
return * stream ;
}
2016-03-14 04:51:04 +08:00
void on_error ( Connection & conn )
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
LOG_DEBUG ( " on_error: " < < conn . id ( ) ) ;
2015-11-23 04:35:40 +08:00
}
2016-03-14 04:51:04 +08:00
void on_wait_timeout ( ) { }
2015-11-23 04:35:40 +08:00
2016-03-14 04:51:04 +08:00
Buffer on_alloc ( Connection & conn )
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
LOG_DEBUG ( " on_alloc socket " < < conn . id ( ) ) ;
2015-11-23 04:35:40 +08:00
return Buffer { buf , sizeof buf } ;
}
2016-03-14 04:51:04 +08:00
void on_read ( Connection & conn , Buffer & buf )
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
LOG_DEBUG ( " on_read socket " < < conn . id ( ) ) ;
auto & socket = conn . socket ;
socket . write ( response . c_str ( ) , response . size ( ) ) ;
2015-11-23 04:35:40 +08:00
}
2016-03-14 04:51:04 +08:00
void on_close ( Connection & conn )
2015-11-23 04:35:40 +08:00
{
2016-03-14 04:51:04 +08:00
LOG_DEBUG ( " on_close socket " < < conn . id ( ) ) ;
conn . close ( ) ;
2015-11-23 04:35:40 +08:00
}
} ;
}