-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudocode.txt
58 lines (46 loc) · 1.51 KB
/
pseudocode.txt
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
MAIN
Verify command line args length (Can't be empty)
parse command line args (url, -h, --help, -b, -B, -o, -O, -d)
parse url into parts ( uri, protocol, site)
Check valid protocol ( http only, no https)
if protocol is empty
protcol = http
if path is empty
path = "/"
if LOOKS LIKE IP ADDRESS ( host) is FALSE
CONVERT HOST TO IP ADDRESS( host, ip_address)
else
ip_address = host
HTTP FETCH AND PRINT ( ip_address, path )
exit
CONVERT HOST TO IP ADDRESS ( host string, ip address )
- Set up the hints structure to specify that we want an IPv4 address and a stream (TCP) socket
- Call getaddrinfo() to resolve the host name to an IP address
- Loop through the results and use the first one we can connect to
- Convert the IP address to a string and copy it to the output buffer
- Return the ip address if found
PARSE URL INTO PARTS:
/*
http://www.doublepup.com/ip.txt
- protocol
- host
- path
<anything up to :// > - protocol
<anything up to / > host
<slash AND anything after it> - path
*/
bool LOOKS LIKE IP ADDRESS ( hostname )
- Use the inet_pton() to convert IP address to binary expression
return true if ip valid, else return false
HTTP FETCH AND PRINT (server_address, page )
create socket
connect to server_address
write GET request for "page"--- using HTTP
read response --- using HTTP
if response code is 2xx
print response text
eles if response code i 3xx
print redirect to host site
else
print code as error message
close socket