dodo  0.0.1
A C++ library to create containerized Linux services
httpversion.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * @file httpversion.cpp
21  * Implements the dodo::network::HTTPVersion class.
22  */
23 
25 
26 #include <common/util.hpp>
27 
28 namespace dodo {
29 
30  namespace network::protocol::http {
31 
33  ParseResult parseResult;
34  enum State : uint8_t {
35  ssStart,
36  ssHttp,
37  sshTtp1,
38  sshTtp2,
39  sshttP,
40  ssSlash,
41  ssMajor,
42  ssDot,
43  ssMinor,
44  ssDone,
45  };
46 
47  uint8_t state = ssStart;
48  std::stringstream major;
49  std::stringstream minor;
50  while ( parseResult.ok() && state != ssDone ) {
51  switch( state ) {
52  case ssStart:
53  if ( ! isspace( buffer.get() ) ) state = ssHttp;
54  else parseResult.setSystemError( buffer.next() );
55  break;
56  case ssHttp:
57  if ( buffer.get() == 'H' ) {
58  parseResult.setSystemError( buffer.next() );
59  state = sshTtp1;
61  break;
62  case sshTtp1:
63  if ( buffer.get() == 'T' ) {
64  parseResult.setSystemError( buffer.next() );
65  state = sshTtp2;
67  break;
68  case sshTtp2:
69  if ( buffer.get() == 'T' ) {
70  parseResult.setSystemError( buffer.next() );
71  state = sshttP;
73  break;
74  case sshttP:
75  if ( buffer.get() == 'P' ) {
76  parseResult.setSystemError( buffer.next() );
77  state = ssSlash;
79  break;
80  case ssSlash:
81  if ( buffer.get() == '/' ) {
82  parseResult.setSystemError( buffer.next() );
83  state = ssMajor;
85  break;
86  case ssMajor:
87  if ( !isdigit( buffer.get() ) ) state = ssDot;
88  else {
89  major << buffer.get();
90  parseResult.setSystemError( buffer.next() );
91  }
92  break;
93  case ssDot:
94  if ( buffer.get() == '.' ) {
95  parseResult.setSystemError( buffer.next() );
96  state = ssMinor;
97  }
99  break;
100  case ssMinor:
101  if ( !isdigit( buffer.get() ) ) state = ssDone;
102  else {
103  minor << buffer.get();
104  parseResult.setSystemError( buffer.next() );
105  }
106  break;
107  }
108  }
109  if ( parseResult.getSystemError() == common::SystemError::ecOK ) {
110  major >> major_;
111  minor >> minor_;
112  }
113  return parseResult;
114  }
115 
116  }
117 
118 }
dodo::network::protocol::http::HTTPVersion::parse
virtual ParseResult parse(VirtualReadBuffer &buffer)
Construct from current position in the VirtualReadBuffer 'HTTP/major_.minor_'.
Definition: httpversion.cpp:32
dodo::network::protocol::http::HTTPFragment::ParseResult::ok
bool ok() const
Test if parseError == peOk && systemError == common::SystemError::ecOK.
Definition: httpfragment.hpp:93
dodo::network::VirtualReadBuffer
Interface to read individual bytes whilst the implementation can read from an actual source (such as ...
Definition: socketreadbuffer.hpp:44
dodo::network::protocol::http::HTTPFragment::ParseResult::setSystemError
void setSystemError(common::SystemError se)
Set the systemError.
Definition: httpfragment.hpp:106
dodo::network::VirtualReadBuffer::next
virtual common::SystemError next()=0
Move to the next char from the VirtualReadBuffer.
dodo::network::VirtualReadBuffer::get
virtual char get() const =0
Get the current char from VirtualReadBuffer.
dodo::network::protocol::http::HTTPVersion::minor_
unsigned int minor_
The minor version.
Definition: httpversion.hpp:115
httpversion.hpp
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::protocol::http::HTTPFragment::ParseResult::getSystemError
common::SystemError getSystemError() const
Return the systemError.
Definition: httpfragment.hpp:116
dodo::network::protocol::http::HTTPFragment::ParseResult
Used to convey parsing succces.
Definition: httpfragment.hpp:75
dodo::common::SystemError::ecOK
@ ecOK
0 Not an error, success
Definition: systemerror.hpp:60
util.hpp
dodo::network::protocol::http::HTTPVersion::major_
unsigned int major_
The major version.
Definition: httpversion.hpp:109
dodo::network::protocol::http::HTTPFragment::peInvalidHTTPVersion
@ peInvalidHTTPVersion
An invalid HTTP version was specified in the request line.
Definition: httpfragment.hpp:61