Skip to content

Commit 820b440

Browse files
committed
Implemented chunked request trailer parsing
1 parent 2786018 commit 820b440

File tree

11 files changed

+159
-102
lines changed

11 files changed

+159
-102
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
- [ ] fcntl subject
55
- [ ] remove inet_ntop
66

7+
- [ ] Is is possible for client to send data whilst cgi is processing?
78
- [ ] (Optional) Fix problem of no server response in case of connection being destructed due to timeout, in every case but especially if cgi is active
89
- [ ] (Optional) Implement try_files or similar
910
- [ ] (Does not occur anymore without having actively tried to fix it) Investigate random closing of program without any notice after sending a first request
1011

12+
- [x] Implement and fix chunked request trailer parsing
1113
- [x] Figure out why resolving symlinks doesnt work
1214
- [x] Replace c function for wildcard pattern matching with own function
1315
- [x] Figure out why login for wordpress doesn't work

include/config/Init.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#ifndef INIT_HPP
22
#define INIT_HPP
33

4-
#include <vector>
5-
6-
#include "../poll/ListenSocket.hpp"
7-
#include "../poll/Poll.hpp"
4+
#include "ListenSocket.hpp"
85
#include "Log.hpp"
6+
#include "Poll.hpp"
97
#include "VirtualHost.hpp"
108

119
class Init {

include/http/Http.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Http : public AConnection {
5050
void OnRequestRecv(std::string msg);
5151
void OnHeadRecv(std::string msg);
5252
void OnChunkSizeRecv(std::string msg);
53+
void OnTrailerRecv(std::string msg);
5354
void OnBodyRecv(std::string msg);
5455
void OnCgiRecv(std::string msg);
5556
void OnCgiError();
@@ -59,7 +60,7 @@ class Http : public AConnection {
5960
void processFile(std::string uri);
6061
void processBodyRequest();
6162
void processPutData(const std::string& data);
62-
void processPostData(std::string& data);
63+
void processPostData(const std::string& data);
6364
void getPutResponse(std::string uri);
6465
void processOptions(std::string uri);
6566
void processDelete(std::string uri);

include/http/Request.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@ class Request {
1414
std::string _method;
1515
Uri _uri;
1616
std::string _version;
17-
std::map<std::string, std::string> _headers;
17+
std::map<std::string, std::list<std::string> > _headers;
1818

1919
public:
2020
Request();
2121
Request(const Request &rhs);
2222
Request &operator=(const Request &rhs);
2323
~Request();
2424

25-
void setHeader(std::string key, std::string value);
25+
void setHeaderField(std::string key, const std::string &value);
26+
void setHeaderFieldValue(std::string key, const std::string &value);
2627

2728
std::string getMethod() const;
2829
Uri &getUri();
2930
std::string getVersion() const;
30-
std::string getHeader(std::string key) const;
31+
std::string getHeaderField(std::string key) const;
3132

32-
void deleteHeaderField(std::string key, std::string value);
33+
void removeHeader(std::string key);
34+
void removeHeaderValue(std::string key, const std::string &value);
35+
36+
bool isMethod(const std::string &method);
37+
bool hasHeaderFieldValue(std::string key, const std::string &value);
3338

3439
int parseStatus(std::string line);
3540
int parseHeaderFields(std::string fields);

include/poll/AConnection.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ class AConnection : public IFileDescriptor {
3535
virtual void OnCgiError() = 0;
3636

3737
protected:
38-
typedef enum state_e { REQUEST_LINE, HEAD, BODY, CHUNK_SIZE } state_t;
38+
typedef enum state_e {
39+
REQUEST_LINE,
40+
HEAD,
41+
BODY,
42+
CHUNK_SIZE,
43+
TRAILER
44+
} state_t;
3945

4046
Address client;
4147
Address host;
@@ -51,6 +57,7 @@ class AConnection : public IFileDescriptor {
5157
virtual void OnRequestRecv(std::string msg) = 0;
5258
virtual void OnHeadRecv(std::string msg) = 0;
5359
virtual void OnChunkSizeRecv(std::string msg) = 0;
60+
virtual void OnTrailerRecv(std::string msg) = 0;
5461
virtual void OnBodyRecv(std::string msg) = 0;
5562
void send(std::istream *msg);
5663
void cgiSend(std::string const &msg);

include/poll/Poll.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef POLL_HPP
22
#define POLL_HPP
33

4-
#include <sys/time.h>
5-
64
#include <csignal>
75
#include <vector>
86

include/utils/utils.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ std::string trim(const std::string& str, std::string chars = " \f\n\r\t\v");
3030
// @exception No custom exceptions
3131
std::string& trimStart(std::string& str, std::string chars = " \f\n\r\t\v");
3232

33+
std::string trimStart(const std::string& str, std::string chars);
34+
3335
// Cuts a string from start to end
3436
// @param str The string to cut from
3537
// @param start The start index
@@ -57,13 +59,21 @@ T split(const std::string& str, std::string delim, bool allowEmpty = false) {
5759
return tokens;
5860
}
5961

60-
// Concatenates a vector of strings into a single string
61-
// @param values The vector of strings
62+
// Concatenates a container of strings into a single string
63+
// @param values The container of strings
6264
// @param separator The separator to use
6365
// @return The concatenated string
6466
// @exception No custom exceptions
65-
std::string concatenate(const std::vector<std::string>& values,
66-
std::string separator);
67+
template <typename T>
68+
std::string concatenate(const T& values, std::string separator) {
69+
std::string value;
70+
typename T::const_iterator it = values.begin();
71+
for (; it != values.end(); it++) {
72+
if (it != values.begin()) value += separator;
73+
value += *it;
74+
}
75+
return value;
76+
}
6777

6878
// Checks if a string starts with a prefix
6979
// @param str The string to check

0 commit comments

Comments
 (0)