-
Notifications
You must be signed in to change notification settings - Fork 0
/
BACKUPMAY14SimpClient.c
115 lines (93 loc) · 2.63 KB
/
BACKUPMAY14SimpClient.c
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
/*------------------------------
* SimpClient.c
* Description: HTTP client program
* CSC 361
* Instructor: Kui Wu
* Adapted from client.c by Teague Lander
-------------------------------*/
#include <stdio.h>
#include <string.h>
/* define maximal string and reply length, this is just an example.*/
/* MAX_RES_LEN should be defined larger (e.g. 4096) in real testing. */
#define MAX_STR_LEN 120
#define MAX_RES_LEN 120
/* Constants */
#define DEFAULT_PORT 80
/* --------- Main() routine ------------
* three main task will be excuted:
* accept the input URI and parse it into fragments for further operation
* open socket connection with specified sockid ID
* use the socket id to connect sopecified server
* don't forget to handle errors
*/
main(int argc, char *argv)
{
char uri[MAX_STR_LEN];
char hostname[MAX_STR_LEN];
char identifier[MAX_STR_LEN];
int sockid, port;
printf("Open URI: ");
scanf("%s", uri);
parse_URI(uri, hostname, &port, identifier);
/*sockid = open_connection(hostname, port);
perform_http(sockid, identifier);*/
}
/*------ Parse an "uri" into "hostname" and resource "identifier" --------*/
parse_URI(char *uri, char *hostname, int *port, char *identifier)
{
printf("parsing URI\n"); //REMOVE THIS
//CHECK IF WE ARE CONNECTING USING HTTP PROTOCOL
char protocol[7];
strncpy(protocol,uri,7);
int test_protocol = strcmp(protocol,"http://");
if (test_protocol != 0)
{
printf("This client only supports the http protocol\n");
}
else
{
printf("HTTP Request.\n");
}
//Get the hostname
int hn_str_pos = strlen(protocol);
for(int i = 0; i < MAX_STR_LEN-hn_str_pos; i++)
{
char next_char = uri[hn_str_pos+i]
if (next_char != '\0') {
hostname[i] = next_char;
break;
}
else if (next_char != ':')
{
}else if (next_char == '/' ) {
}else {
hostname[i] = next_char;
}
}
printf("%s\n",hostname);
printf("hello\n");
printf("returning parsed URI\n"); //REMOVE THIS
return;
}
/*------------------------------------*
* connect to a HTTP server using hostname and port,
* and get the resource specified by identifier
*--------------------------------------*/
perform_http(int sockid, char *identifier)
{
/* connect to server and retrieve response */
close(sockid);
}
/*---------------------------------------------------------------------------*
*
* open_conn() routine. It connects to a remote server on a specified port.
*
*---------------------------------------------------------------------------*/
int open_connection(char *hostname, int port)
{
int sockfd;
/* generate socket
* connect socket to the host address
*/
return sockfd;
}