-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp333d.cc
128 lines (110 loc) · 3.68 KB
/
http333d.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright ©2022 Hal Perkins. All rights reserved. Permission is
* hereby granted to students registered for University of Washington
* CSE 333 for use solely during Spring Quarter 2022 for purposes of
* the course. No other use, copying, distribution, or modification
* is permitted without prior written consent. Copyrights for
* third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the
* author.
*/
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <list>
#include "./ServerSocket.h"
#include "./HttpServer.h"
using std::cerr;
using std::cout;
using std::endl;
using std::list;
using std::string;
// Print out program usage, and exit() with EXIT_FAILURE.
static void Usage(char* prog_name);
// Parse command-line arguments to get port, path, and indices to use
// for your http333d server.
//
// Params:
// - argc: number of argumnets
// - argv: array of arguments
// - port: output parameter returning the port number to listen on
// - path: output parameter returning the directory with our static files
// - indices: output parameter returning the list of index file names
//
// Calls Usage() on failure. Possible errors include:
// - path is not a readable directory
// - index file names are readable
static void GetPortAndPath(int argc,
char** argv,
uint16_t* const port,
string* const path,
list<string>* const indices);
int main(int argc, char** argv) {
// Print out welcome message.
cout << "Welcome to http333d, the UW cse333 web server!" << endl;
cout << " Copyright 2012 Steven Gribble" << endl;
cout << " http://www.cs.washington.edu/homes/gribble" << endl;
cout << endl;
cout << "initializing:" << endl;
cout << " parsing port number and static files directory..." << endl;
// Ignore the SIGPIPE signal, otherwise we'll crash out if a client
// disconnects unexpectedly.
signal(SIGPIPE, SIG_IGN);
// Get the port number and list of index files.
uint16_t port_num;
string static_dir;
list<string> indices;
GetPortAndPath(argc, argv, &port_num, &static_dir, &indices);
cout << " port: " << port_num << endl;
cout << " path: " << static_dir << endl;
// Run the server.
hw4::HttpServer hs(port_num, static_dir, indices);
if (!hs.Run()) {
cerr << " server failed to run!?" << endl;
}
cout << "server completed! Exiting." << endl;
return EXIT_SUCCESS;
}
static void Usage(char* prog_name) {
cerr << "Usage: " << prog_name << " port staticfiles_directory indices+";
cerr << endl;
exit(EXIT_FAILURE);
}
static void GetPortAndPath(int argc,
char** argv,
uint16_t* const port,
string* const path,
list<string>* const indices) {
// Here are some considerations when implementing this function:
// - There is a reasonable number of command line arguments
// - The port number is reasonable
// - The path (i.e., argv[2]) is a readable directory
// - You have at least 1 index, and all indices are readable files
// STEP 1:
if (argc < 4) {
Usage(argv[0]);
}
uint16_t p = static_cast<uint16_t>(atoi(argv[1]));
if (p < 0 || p > 65535) {
Usage(argv[0]);
}
*port = p;
int sipapi = access(argv[2], R_OK);
if (sipapi == -1) {
Usage(argv[0]);
}
*path = std::string(argv[2]);
for (int i = 3; i < argc; i++) {
int yesmami = access(argv[i], R_OK);
if (yesmami == -1) {
Usage(argv[0]);
}
(*indices).push_back(argv[i]);
}
}