-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.c
More file actions
119 lines (100 loc) · 2.91 KB
/
Copy pathproxy.c
File metadata and controls
119 lines (100 loc) · 2.91 KB
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
/*
* proxy.c - CS:APP Web proxy
*
* TEAM MEMBERS:
*
* IMPORTANT: Give a high level description of your code here. You
* must also provide a header comment at the beginning of each
* function that describes what that function does.
*/
#include "csapp.h"
#include "core/main.h"
/*
* Function prototypes
*/
int parse_uri(char *uri, char *target_addr, char *path, int *port);
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr, char *uri, int size);
/*
* main - Main routine for the proxy program
*/
int main(int argc, char **argv)
{
/* Check arguments */
/* if (argc != 2) {
# fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
exit(0);
}
// fprintf(stderr, "Not Implemented\n");
*/
return proxy_main(argc, argv);
}
/*
* parse_uri - URI parser
*
* Given a URI from an HTTP proxy GET request (i.e., a URL), extract
* the host name, path name, and port. The memory for hostname and
* pathname must already be allocated and should be at least MAXLINE
* bytes. Return -1 if there are any problems.
*/
int parse_uri(char *uri, char *hostname, char *pathname, int *port)
{
char *hostbegin;
char *hostend;
char *pathbegin;
int len;
if (strncasecmp(uri, "http://", 7) != 0) {
hostname[0] = '\0';
return -1;
}
/* Extract the host name */
hostbegin = uri + 7;
hostend = strpbrk(hostbegin, " :/\r\n\0");
len = hostend - hostbegin;
strncpy(hostname, hostbegin, len);
hostname[len] = '\0';
/* Extract the port number */
*port = 80; /* default */
if (*hostend == ':')
*port = atoi(hostend + 1);
/* Extract the path */
pathbegin = strchr(hostbegin, '/');
if (pathbegin == NULL) {
pathname[0] = '\0';
}
else {
pathbegin++;
strcpy(pathname, pathbegin);
}
return 0;
}
/*
* format_log_entry - Create a formatted log entry in logstring.
*
* The inputs are the socket address of the requesting client
* (sockaddr), the URI from the request (uri), and the size in bytes
* of the response from the server (size).
*/
void format_log_entry(char *logstring, struct sockaddr_in *sockaddr,
char *uri, int size)
{
time_t now;
char time_str[MAXLINE];
unsigned long host;
unsigned char a, b, c, d;
/* Get a formatted time string */
now = time(NULL);
strftime(time_str, MAXLINE, "%a %d %b %Y %H:%M:%S %Z", localtime(&now));
/*
* Convert the IP address in network byte order to dotted decimal
* form. Note that we could have used inet_ntoa, but chose not to
* because inet_ntoa is a Class 3 thread unsafe function that
* returns a pointer to a static variable (Ch 13, CS:APP).
*/
host = ntohl(sockaddr->sin_addr.s_addr);
a = host >> 24;
b = (host >> 16) & 0xff;
c = (host >> 8) & 0xff;
d = host & 0xff;
/* Return the formatted log entry string */
sprintf(logstring, "%s: %d.%d.%d.%d %s", time_str, a, b, c, d, uri);
}