Skip to content
Snippets Groups Projects
Commit 73515ce3 authored by Mengqi Chen's avatar Mengqi Chen
Browse files

update find newline

parent 422034ae
No related branches found
No related tags found
No related merge requests found
......@@ -125,6 +125,11 @@ int main(int argc, char **argv) {
// read file path from client and do path validation
string base_dir(".");
if (!IsPathSafe(base_dir, req_file_path)) {
perror(" Path is not safe");
close(new_client_fd);
continue;
}
// all good, dispatch thread and setup pollfd
cout << " New incoming connection - " << new_client_fd << "\n";
......@@ -160,7 +165,7 @@ int main(int argc, char **argv) {
Work *thread_work = worker_to_client[events[i].fd];
if (thread_work == nullptr) {
cerr << " worker_to_client mapping corrupted for fd: " << events[i].fd << "\n";
// TODO: need some action
continue;
}
// return data to client
......
......@@ -13,86 +13,6 @@
using namespace std;
#define BUFFER_SIZE 4096
int Listen(char *portnum) {
// Populate the "hints" addrinfo structure for getaddrinfo().
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; // IPv6 (also handles IPv4 clients)
hints.ai_socktype = SOCK_STREAM; // stream
hints.ai_flags = AI_PASSIVE; // use wildcard "INADDR_ANY"
hints.ai_flags |= AI_V4MAPPED; // use v4-mapped v6 if no v6 found
hints.ai_protocol = IPPROTO_TCP; // tcp protocol
hints.ai_canonname = nullptr;
hints.ai_addr = nullptr;
hints.ai_next = nullptr;
// use the portnum string representation to
// pass in to getaddrinfo(). getaddrinfo() returns a list of
// address structures via the output parameter "result".
struct addrinfo *result;
int res = getaddrinfo(nullptr, portnum, &hints, &result);
// check if getaddrinfo failed
if (res != 0) {
std::cerr << "getaddrinfo() failed: ";
std::cerr << gai_strerror(res) << std::endl;
return -1;
}
// Loop through the returned address structures until we are able
// to create a socket and bind to one. The address structures are
// linked in a list through the "ai_next" field of result.
int listen_fd = -1;
for (struct addrinfo *rp = result; rp != nullptr; rp = rp->ai_next) {
listen_fd = socket(rp->ai_family,
rp->ai_socktype,
rp->ai_protocol);
if (listen_fd == -1) {
// Creating this socket failed. So, loop to the next returned
// result and try again.
std::cerr << "socket() failed: " << strerror(errno) << std::endl;
listen_fd = -1;
continue;
}
// Configure the socket; we're setting a socket "option." In
// particular, we set "SO_REUSEADDR", which tells the TCP stack
// so make the port we bind to available again as soon as we
// exit, rather than waiting for a few tens of seconds to recycle it.
int optval = 1;
assert(setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval)) == 0);
// Try binding the socket to the address and port number returned
// by getaddrinfo().
if (bind(listen_fd, rp->ai_addr, rp->ai_addrlen) == 0) {
break;
}
// The bind failed. Close the socket, then loop back around and
// try the next address/port returned by getaddrinfo().
close(listen_fd);
listen_fd = -1;
}
// Free the structure returned by getaddrinfo().
freeaddrinfo(result);
// If we failed to bind, return failure.
if (listen_fd <= 0)
return listen_fd;
// Success. Tell the OS that we want this to be a listening socket.
if (listen(listen_fd, SOMAXCONN) != 0) {
std::cerr << "Failed to mark socket as listening: ";
std::cerr << strerror(errno) << std::endl;
close(listen_fd);
return -1;
}
return listen_fd;
}
int WrappedRead(int fd, string *file_path) {
int read_res;
int bytes_read = 0;
......@@ -118,13 +38,8 @@ int WrappedRead(int fd, string *file_path) {
file_path->append((char*)buffer, read_res);
int endline_idx = file_path->find('\n');
while (endline_idx != -1) {
file_path->erase(endline_idx, 1);
endline_idx = file_path->find('\n');
found_endline = true;
}
if (found_endline) {
if (endline_idx != -1) {
*file_path = file_path->substr(0, endline_idx);
break;
}
}
......
......@@ -8,10 +8,6 @@ using namespace std;
static const int PATH_MAX = 254;
// accepts a char*/string representation of a portnum
// it returns a listen file descriptor to use
int Listen(char *portnum);
// Wrapper function around read that deals with reading errors.
// Read at most readlen bytes from fd and stores it into buf.
// on success it returns the number of bytes that
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment