1 | // |
2 | // HTTPHeaderStream.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: HTTP |
6 | // Module: HTTPHeaderStream |
7 | // |
8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/HTTPHeaderStream.h" |
16 | #include "Poco/Net/HTTPSession.h" |
17 | |
18 | |
19 | namespace Poco { |
20 | namespace Net { |
21 | |
22 | |
23 | // |
24 | // HTTPHeaderStreamBuf |
25 | // |
26 | |
27 | |
28 | HTTPHeaderStreamBuf::(HTTPSession& session, openmode mode): |
29 | HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode), |
30 | _session(session), |
31 | _end(false) |
32 | { |
33 | } |
34 | |
35 | |
36 | HTTPHeaderStreamBuf::() |
37 | { |
38 | } |
39 | |
40 | |
41 | int HTTPHeaderStreamBuf::(char* buffer, std::streamsize length) |
42 | { |
43 | // read line-by-line; an empty line denotes the end of the headers. |
44 | static const int eof = std::char_traits<char>::eof(); |
45 | |
46 | if (_end) return 0; |
47 | |
48 | int n = 0; |
49 | int ch = _session.get(); |
50 | while (ch != eof && ch != '\n' && n < length - 1) |
51 | { |
52 | *buffer++ = (char) ch; ++n; |
53 | ch = _session.get(); |
54 | } |
55 | if (ch != eof) |
56 | { |
57 | *buffer++ = (char) ch; ++n; |
58 | if (n == 2) _end = true; |
59 | } |
60 | return n; |
61 | } |
62 | |
63 | |
64 | int HTTPHeaderStreamBuf::(const char* buffer, std::streamsize length) |
65 | { |
66 | return _session.write(buffer, length); |
67 | } |
68 | |
69 | |
70 | // |
71 | // HTTPHeaderIOS |
72 | // |
73 | |
74 | |
75 | HTTPHeaderIOS::(HTTPSession& session, HTTPHeaderStreamBuf::openmode mode): |
76 | _buf(session, mode) |
77 | { |
78 | poco_ios_init(&_buf); |
79 | } |
80 | |
81 | |
82 | HTTPHeaderIOS::() |
83 | { |
84 | try |
85 | { |
86 | _buf.sync(); |
87 | } |
88 | catch (...) |
89 | { |
90 | } |
91 | } |
92 | |
93 | |
94 | HTTPHeaderStreamBuf* HTTPHeaderIOS::() |
95 | { |
96 | return &_buf; |
97 | } |
98 | |
99 | |
100 | // |
101 | // HTTPHeaderInputStream |
102 | // |
103 | |
104 | |
105 | Poco::MemoryPool HTTPHeaderInputStream::(sizeof(HTTPHeaderInputStream)); |
106 | |
107 | |
108 | HTTPHeaderInputStream::(HTTPSession& session): |
109 | HTTPHeaderIOS(session, std::ios::in), |
110 | std::istream(&_buf) |
111 | { |
112 | } |
113 | |
114 | |
115 | HTTPHeaderInputStream::() |
116 | { |
117 | } |
118 | |
119 | |
120 | void* HTTPHeaderInputStream::(std::size_t /*size*/) |
121 | { |
122 | return _pool.get(); |
123 | } |
124 | |
125 | |
126 | void HTTPHeaderInputStream::(void* ptr) |
127 | { |
128 | try |
129 | { |
130 | _pool.release(ptr); |
131 | } |
132 | catch (...) |
133 | { |
134 | poco_unexpected(); |
135 | } |
136 | } |
137 | |
138 | |
139 | // |
140 | // HTTPHeaderOutputStream |
141 | // |
142 | |
143 | |
144 | Poco::MemoryPool HTTPHeaderOutputStream::(sizeof(HTTPHeaderOutputStream)); |
145 | |
146 | |
147 | HTTPHeaderOutputStream::(HTTPSession& session): |
148 | HTTPHeaderIOS(session, std::ios::out), |
149 | std::ostream(&_buf) |
150 | { |
151 | } |
152 | |
153 | |
154 | HTTPHeaderOutputStream::() |
155 | { |
156 | } |
157 | |
158 | |
159 | void* HTTPHeaderOutputStream::(std::size_t /*size*/) |
160 | { |
161 | return _pool.get(); |
162 | } |
163 | |
164 | |
165 | void HTTPHeaderOutputStream::(void* ptr) |
166 | { |
167 | try |
168 | { |
169 | _pool.release(ptr); |
170 | } |
171 | catch (...) |
172 | { |
173 | poco_unexpected(); |
174 | } |
175 | } |
176 | |
177 | |
178 | } } // namespace Poco::Net |
179 | |