Skip to content

Commit 18a2d9a

Browse files
committed
Merge branch 'main' into pythongermany
2 parents 8f07f02 + d1bfccf commit 18a2d9a

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
lines changed

include/poll/Address.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#ifndef ADDRESS_HPP
22
#define ADDRESS_HPP
33

4-
#include <arpa/inet.h> //inet_*
54
#include <netdb.h>
65
#include <netinet/in.h>
76
#include <sys/socket.h> //AF_INET
87

98
#include <cerrno>
109
#include <cstdlib> //atoi
1110
#include <cstring>
11+
#include <iomanip>
1212
#include <limits>
1313
#include <ostream> //overload
1414
#include <set>
@@ -39,7 +39,6 @@ class Address {
3939
void size(socklen_t size);
4040
void port(in_port_t port);
4141
in_port_t port() const;
42-
std::string str() const;
4342

4443
bool operator<(Address const &other) const;
4544
static std::set<Address> resolveHost(std::string const &src);

src/http/Http.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ void Http::processCgi(std::string contentLength) {
288288

289289
env.push_back("SERVER_NAME=" + _request.getHeaderField("Host"));
290290
env.push_back("SERVER_PORT=" + toString<in_port_t>(host.port()));
291-
env.push_back("REMOTE_ADDR=" + client.str());
291+
std::ostringstream oss;
292+
oss << "REMOTE_ADDR=" << client;
293+
env.push_back(oss.str());
292294

293295
// Optional stuff to increase functionality
294296
env.push_back("HTTP_COOKIE=" + _request.getHeaderField("Cookie"));

src/poll/AConnection.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ void AConnection::send(std::istream *msg) {
9292
}
9393
}
9494

95-
/**
96-
* TODO: callback function for POLLERR and POLLHUP?
97-
*/
9895
void AConnection::onPollEvent(struct pollfd &pollfd,
9996
CallbackPointer *newCallbackObject,
10097
struct pollfd *newPollfd) {
@@ -133,7 +130,7 @@ void AConnection::onPipeOutPollEvent(struct pollfd &pollfd) {
133130
}
134131
if (pollfd.events == 0) {
135132
pipeOut = -1;
136-
_cgiWriteBuffer.clear(); // TODO: unecessary?
133+
_cgiWriteBuffer.clear();
137134
}
138135
}
139136

@@ -238,7 +235,7 @@ void AConnection::onPipeInPollIn(struct pollfd &pollfd) {
238235
} else if (ret == 0) {
239236
int status;
240237

241-
waitpid(_cgiPid, &status, 0); // TODO: error checking?
238+
waitpid(_cgiPid, &status, 0);
242239
pid_t tmp = _cgiPid;
243240
_cgiPid = -1;
244241
accessLog_g.write(

src/poll/Address.cpp

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,89 @@ bool Address::operator==(Address const &other) const {
194194
}
195195
}
196196

197-
std::string Address::str() const {
198-
char buffer[INET6_ADDRSTRLEN];
199-
200-
if (!inet_ntop(_family, addr(), buffer, INET6_ADDRSTRLEN))
201-
throw std::runtime_error(std::string("Address::str(): ") +
202-
std::strerror(errno));
203-
if (_family == AF_INET6) {
204-
std::string result("[");
205-
result.append(buffer);
206-
result.push_back(']');
207-
return result;
197+
static std::ostream &operator<<(std::ostream &os, in_addr const &src) {
198+
int32_t hostByteOrder = ntohl(src.s_addr);
199+
os << (hostByteOrder >> 24 & 0xFF) << '.';
200+
os << (hostByteOrder >> 16 & 0xFF) << '.';
201+
os << (hostByteOrder >> 8 & 0xFF) << '.';
202+
os << (hostByteOrder & 0xFF);
203+
return os;
204+
}
205+
206+
static std::pair<unsigned int, unsigned int> allZeroFields(
207+
unsigned char const *addr) {
208+
unsigned int currentStart = 0;
209+
unsigned int currentSize = 0;
210+
std::pair<unsigned int, unsigned int> result;
211+
unsigned int bestSize = 0;
212+
213+
for (unsigned int i = 0; i < 8; ++i) {
214+
unsigned int pos = i * 2;
215+
if (addr[pos] != 0 || addr[pos + 1] != 0) {
216+
currentSize = 0;
217+
continue;
218+
}
219+
if (currentSize == 0) {
220+
currentSize = 1;
221+
currentStart = pos;
222+
} else {
223+
++currentSize;
224+
}
225+
if (currentSize > bestSize) {
226+
bestSize = currentSize;
227+
result.first = currentStart;
228+
}
208229
}
209-
return std::string(buffer);
230+
231+
if (bestSize) {
232+
result.second = result.first + bestSize * 2;
233+
result.second -= 1;
234+
if (result.second - result.first == 1) result.second = 0;
235+
}
236+
return result;
210237
}
211238

212-
/**
213-
* @throw std::runtime_error() if addr.family() is not AF_INET or AF_INET6
214-
*/
215-
std::ostream &operator<<(std::ostream &os, Address const &addr) {
216-
char buffer[INET6_ADDRSTRLEN];
239+
static std::ostream &operator<<(std::ostream &os, in6_addr const &src) {
240+
unsigned char const *addr = reinterpret_cast<unsigned char const *>(&src);
217241

218-
if (!inet_ntop(addr.family(), addr.addr(), buffer, INET6_ADDRSTRLEN))
219-
throw std::runtime_error(std::string("<<Address: ") + std::strerror(errno));
220-
if (addr.family() == AF_INET) {
221-
os << buffer << ":" << addr.port();
222-
} else {
223-
os << "[" << buffer << "]:" << addr.port();
242+
os << '[';
243+
if ((IN6_IS_ADDR_V4COMPAT(addr) && (addr[12] != 0 || addr[13] != 0)) ||
244+
IN6_IS_ADDR_V4MAPPED(addr)) {
245+
addr[10] == 255 ? os << "::ffff:" : os << "::";
246+
os << *reinterpret_cast<in_addr const *>(&addr[12]);
247+
os << ']';
248+
return os;
224249
}
250+
251+
std::pair<unsigned int, unsigned int> zeroField = allZeroFields(addr);
252+
std::ios_base::fmtflags flags =
253+
os.setf(std::ios_base::hex, std::ios_base::basefield);
254+
255+
for (size_t i = 0; i < 16; ++i) {
256+
bool first = (i % 2 == 0);
257+
258+
if (zeroField.second == 0 || i < zeroField.first || i > zeroField.second) {
259+
if (!first) {
260+
if (addr[i - 1] != 0) os << std::setw(2) << std::setfill('0');
261+
os << (int)addr[i];
262+
if (i != 15) os << ':';
263+
} else if (addr[i] != 0) {
264+
os << (int)addr[i];
265+
}
266+
} else if (i == zeroField.second || (i == 0 && zeroField.first == 0)) {
267+
os << ':';
268+
}
269+
}
270+
os.flags(flags);
271+
os << ']';
272+
return os;
273+
}
274+
275+
std::ostream &operator<<(std::ostream &os, Address const &addr) {
276+
if (addr.family() == AF_INET6)
277+
os << *reinterpret_cast<in6_addr const *>(addr.addr());
278+
else
279+
os << *reinterpret_cast<in_addr const *>(addr.addr());
280+
os << ':' << addr.port();
225281
return os;
226282
}

0 commit comments

Comments
 (0)