-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.h
144 lines (133 loc) · 3.08 KB
/
http.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef HTTP_H
#define HTTP_H
#define _XOPEN_SOURCE 700
/*
* http.h
* simple and small http library for C99
* version 0.1.2
* creator: kocotian
*/
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#ifndef MAX_REQUEST_LEN
#define MAX_REQUEST_LEN 1024
#endif
long long int httpGET(char *hostname, unsigned short port, char *path, char **buffer);
int getResponseStatus(char *response);
int parseResponseLine(char *response, char *value, char **buffer);
char *truncateHeader(char *response);
int getHeaderLength(char *response);
long long int
httpGET(char *hostname, unsigned short port, char *path, char **buffer)
{
char tmpbuf[BUFSIZ];
struct hostent *hostent;
struct sockaddr_in sockaddr_in;
in_addr_t in_addr;
int sockfd;
char *request_template = "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n";
char request[MAX_REQUEST_LEN];
int request_length;
int byteswrote = 0;
long long int totalbytes = 0;
int writeiter = 0;
request_length = snprintf(request, MAX_REQUEST_LEN, request_template,
path, hostname);
if((hostent = gethostbyname(hostname)) == NULL)
{
fprintf(stderr, "gethostbyname(%s) error", hostname);
return NULL;
}
if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
perror("socket opening error");
return NULL;
}
in_addr = inet_addr(inet_ntoa(*(struct in_addr*)*(hostent -> h_addr_list)));
sockaddr_in.sin_port = htons(port);
sockaddr_in.sin_addr.s_addr = in_addr;
sockaddr_in.sin_family = AF_INET;
if(connect(sockfd, (struct sockaddr*)&sockaddr_in, sizeof(sockaddr_in)) == -1)
{
perror("socket connecting error");
return NULL;
}
if((write(sockfd, request, request_length)) == -1)
{
perror("sending request error");
return NULL;
}
*buffer = malloc(BUFSIZ);
while((byteswrote = read(sockfd, tmpbuf, BUFSIZ)) > 0)
{
totalbytes += byteswrote;
strncat(*buffer, tmpbuf, byteswrote);
*buffer = realloc(*buffer, BUFSIZ * ((++writeiter) + 1));
}
close(sockfd);
return totalbytes;
}
int
getResponseStatus(char *response)
{
char *firstline = strtok(response, "\r"); char *status;
firstline = strtok(response, "\r");
status = strtok(firstline, " ");
status = strtok(NULL, " ");
return atoi(status);
}
int
parseResponseLine(char *response, char *value, char **buffer)
{
char *tmpbuf = strtok(response, "\r");
char *val;
if(!strcmp(value, "HTTP"))
{
*buffer = tmpbuf;
return 0;
}
while(strcmp(tmpbuf, "\n"))
{
tmpbuf = strtok(NULL, "\r");
unsigned short iter = -1;
char allOk = 1;
while(value[++iter])
{
if(value[iter] != tmpbuf[iter + 1])
{
allOk = 0;
break;
}
else allOk = 1;
}
if(allOk)
{
iter += 2;
int i2 = 1;
*buffer = malloc(i2);
while(tmpbuf[iter])
{
(*buffer)[i2 - 1] = tmpbuf[++iter];
*buffer = realloc(*buffer, ++i2);
}
(*buffer)[i2 - 2] = 0;
return 0;
}
}
return -1;
}
char *truncateHeader(char *response)
{
return strstr(response, "\r\n\r\n") + 4;
}
int getHeaderLength(char *response)
{
return strstr(response, "\r\n\r\n") - response;
}
#endif