ScummVM API documentation
reader.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BACKENDS_NETWORKING_SDL_NET_READER_H
23 #define BACKENDS_NETWORKING_SDL_NET_READER_H
24 
25 #include "common/str.h"
26 #include "common/hashmap.h"
27 #include "common/hash-str.h"
28 
29 namespace Common {
30 class MemoryReadWriteStream;
31 class WriteStream;
32 }
33 
34 namespace Networking {
35 
36 enum ReaderState {
37  RS_NONE,
38  RS_READING_HEADERS,
39  RS_READING_CONTENT
40 };
41 
42 enum ReaderMode {
43  RM_HTTP_GENERIC,
44  RM_POST_FORM_MULTIPART
45 };
46 
79 class Reader {
80  ReaderState _state;
81  ReaderMode _mode;
83  uint32 _bytesLeft;
84 
85  byte *_window;
86  uint32 _windowUsed, _windowSize, _windowReadPosition, _windowWritePosition;
87  uint32 _windowHash;
88 
89  Common::MemoryReadWriteStream *_headersStream;
90 
91  Common::String _headers;
92  Common::String _method, _path, _query, _anchor;
94  uint32 _contentLength;
95  Common::String _boundary;
96  uint32 _availableBytes;
97  bool _firstBlock;
98  bool _isBadRequest;
99  bool _allContentRead;
100 
101  void cleanup();
102 
103  bool readAndHandleFirstHeaders(); //true when ended reading
104  bool readBlockHeadersIntoStream(Common::WriteStream *stream); //true when ended reading
105  bool readContentIntoStream(Common::WriteStream *stream); //true when ended reading
106 
107  void handleFirstHeaders(Common::MemoryReadWriteStream *headers);
108  void parseFirstLine(const Common::String &headers);
109  void parsePathQueryAndAnchor(const Common::String &pathToParse);
110  void parseQueryParameters();
111 
112  void makeWindow(uint32 size);
113  void freeWindow();
114  bool readOneByteInStream(Common::WriteStream *stream, const Common::String &boundary, const uint32 boundaryHash);
115 
116  byte readOne();
117  uint32 bytesLeft() const;
118 
119 public:
120  static const int32 SUSPICIOUS_HEADERS_SIZE = 1024 * 1024; // 1 MB is really a lot
121 
122  Reader();
123  ~Reader();
124 
125  Reader &operator=(Reader &r);
126 
127  bool readFirstHeaders(); //true when ended reading
128  bool readContent(Common::WriteStream *stream); //true when ended reading
129  bool readFirstContent(Common::WriteStream *stream); //true when ended reading
130  bool readBlockHeaders(Common::WriteStream *stream); //true when ended reading
131  bool readBlockContent(Common::WriteStream *stream); //true when ended reading
132 
133  void setMode(ReaderMode mode);
134  void setContent(Common::MemoryReadWriteStream *stream);
135 
136  bool badRequest() const;
137  bool noMoreContent() const;
138 
139  Common::String headers() const;
140  Common::String method() const;
141  Common::String path() const;
142  Common::String query() const;
143  Common::String queryParameter(const Common::String &name) const;
144  Common::String anchor() const;
145 
146  static Common::String readEverythingFromMemoryStream(Common::MemoryReadWriteStream *stream);
147 };
148 
149 } // End of namespace Networking
150 
151 #endif
Definition: str.h:59
Definition: stream.h:77
Definition: scummvmcloud.h:31
Definition: reader.h:79
Definition: algorithm.h:29
Definition: memstream.h:283