-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsget.c
More file actions
344 lines (290 loc) · 9.41 KB
/
httpsget.c
File metadata and controls
344 lines (290 loc) · 9.41 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <ctype.h>
#include "ssl.h"
#include "iconv/iconv_mini.h"
char request[65536];
char response[65536];
/*
* Shift_JISの文字かどうかを判定
*/
int is_sjis_lead_byte(unsigned char c) {
return ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xFC));
}
/*
* ブロック要素のHTMLタグかどうかを判定
*/
int is_block_element(const char *tag_start) {
const char *block_tags[] = {
"<div", "<p", "<h1", "<h2", "<h3", "<h4", "<h5", "<h6",
"<ul", "<ol", "<li", "<dl", "<dt", "<dd",
"<table", "<tr", "<td", "<th", "<thead", "<tbody", "<tfoot",
"<article", "<section", "<header", "<footer", "<nav", "<aside",
"<blockquote", "<pre", "<address", "<hr", "<br",
"</div>", "</p>", "</h1>", "</h2>", "</h3>", "</h4>", "</h5>", "</h6>",
"</ul>", "</ol>", "</li>", "</dl>", "</dt>", "</dd>",
"</table>", "</tr>", "</td>", "</th>", "</thead>", "</tbody>", "</tfoot>",
"</article>", "</section>", "</header>", "</footer>", "</nav>", "</aside>",
"</blockquote>", "</pre>", "</address>", "</hr>", "</br>",
NULL
};
int i;
for (i = 0; block_tags[i] != NULL; i++) {
int len = strlen(block_tags[i]);
if (strncmp(tag_start, block_tags[i], len) == 0) {
return 1;
}
}
return 0;
}
/*
* 簡単なHTMLパース処理(Shift_JIS対応)
* HTMLタグを除去してプレーンテキストを抽出
*/
void parse_html(char *html_content) {
unsigned char *src = (unsigned char*)html_content;
unsigned char *dst = (unsigned char*)html_content;
int in_tag = 0;
int in_script = 0;
int in_style = 0;
int consecutive_spaces = 0;
unsigned char *tag_start = NULL;
while (*src) {
// Shift_JISの2バイト文字の処理
if (is_sjis_lead_byte(*src) && *(src + 1)) {
if (!in_tag && !in_script && !in_style) {
*dst++ = *src;
*dst++ = *(src + 1);
consecutive_spaces = 0;
}
src += 2;
continue;
}
// スクリプトタグの検出
if (!in_tag && strncmp((char*)src, "<script", 7) == 0) {
in_script = 1;
in_tag = 1;
tag_start = src;
src += 7;
continue;
} else if (in_script && strncmp((char*)src, "</script>", 9) == 0) {
in_script = 0;
in_tag = 0;
src += 9;
continue;
}
// スタイルタグの検出
if (!in_tag && strncmp((char*)src, "<style", 6) == 0) {
in_style = 1;
in_tag = 1;
tag_start = src;
src += 6;
continue;
} else if (in_style && strncmp((char*)src, "</style>", 8) == 0) {
in_style = 0;
in_tag = 0;
src += 8;
continue;
}
// タグの開始
if (*src == '<' && !in_script && !in_style) {
in_tag = 1;
tag_start = src;
}
// タグの終了
else if (*src == '>' && in_tag && !in_script && !in_style) {
in_tag = 0;
// ブロック要素の場合は改行を追加
if (tag_start && is_block_element((char*)tag_start)) {
// 直前が改行でない場合のみ改行を追加
if (dst > (unsigned char*)html_content && *(dst - 1) != '\n') {
*dst++ = '\n';
consecutive_spaces = 0;
}
}
src++;
continue;
}
// タグ内でない場合は文字をコピー
if (!in_tag && !in_script && !in_style) {
// HTMLエンティティの簡単な変換
if (strncmp((char*)src, "<", 4) == 0) {
*dst++ = '<';
src += 4;
consecutive_spaces = 0;
} else if (strncmp((char*)src, ">", 4) == 0) {
*dst++ = '>';
src += 4;
consecutive_spaces = 0;
} else if (strncmp((char*)src, "&", 5) == 0) {
*dst++ = '&';
src += 5;
consecutive_spaces = 0;
} else if (strncmp((char*)src, """, 6) == 0) {
*dst++ = '"';
src += 6;
consecutive_spaces = 0;
} else if (strncmp((char*)src, " ", 6) == 0) {
*dst++ = ' ';
src += 6;
consecutive_spaces = 1;
} else if (isspace(*src)) {
// 連続する空白文字を1つのスペースに変換
if (!consecutive_spaces) {
*dst++ = ' ';
consecutive_spaces = 1;
}
src++;
} else {
*dst++ = *src++;
consecutive_spaces = 0;
}
} else {
src++;
}
}
*dst = '\0';
}
/*
* HTTPレスポンスからヘッダーとボディを分離
*/
char* extract_html_body(char *response) {
char *body = strstr(response, "\r\n\r\n");
if (body) {
return body + 4;
}
body = strstr(response, "\n\n");
if (body) {
return body + 2;
}
return response;
}
/****************************************************************************/
int main(int argc, char **argv)
{
int client_fd;
struct sockaddr_in server_addr;
struct hostent *host_entry;
char *hostname = NULL;
char *path = NULL;
int port = 0;
int bytes_sent, bytes_received;
int total_received = 0;
int status_code;
int i;
// デフォルト値のリセット(オプション処理後)
hostname = "www.yahoo.co.jp";
path = "/";
port = 443;
if (argc >= 2) {
hostname = argv[1];
}
if (argc >= 3) {
path = argv[2];
}
printf("Connecting to %s:%d%s\n", hostname, port, path);
// ホスト名の解決
host_entry = gethostbyname(hostname);
if (host_entry == NULL) {
printf("gethostbyname() failed for %s\n", hostname);
return 1;
}
printf("Host resolved to: %s\n", inet_ntoa(*(struct in_addr*)host_entry->h_addr_list[0]));
// ソケットの作成
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket() failed");
return 1;
}
printf("Socket created successfully\n");
// サーバーアドレスの設定
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
memcpy(&server_addr.sin_addr, host_entry->h_addr_list[0], host_entry->h_length);
// サーバーに接続
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect() failed");
close(client_fd);
return 1;
}
printf("Connected to server successfully\n");
SSL_CTX *ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS);
SSL_EXTENSIONS *ext = ssl_ext_new();
SSL *ssl_sock = ssl_client_new(ssl_ctx, client_fd, NULL, 0, ext);
int r = ssl_handshake_status(ssl_sock);
printf("Handshake status: %d\n", r);
// HTTP GETリクエストの作成
snprintf(request, sizeof(request),
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-Agent: X68000-HTTPS-Client/1.0\r\n"
"Accept: */*\r\n"
"Connection: close\r\n"
"\r\n",
path, hostname);
printf("Sending HTTP request:\n");
printf("--------------------\n");
printf("%s", request);
printf("--------------------\n\n");
// HTTPリクエストを送信
bytes_sent = ssl_write(ssl_sock, request, strlen(request));
if (bytes_sent < 0) {
perror("send() failed");
close(client_fd);
return 1;
}
printf("Sent %d bytes\n", bytes_sent);
printf("Receiving HTTP response...\n\n");
// HTTPレスポンスを受信してバッファに蓄積
memset(response, 0, sizeof(response));
total_received = 0;
uint8_t *buffer;
size_t buffer_size;
buffer = malloc(1);
*buffer = 0;
buffer_size = 1;
int res;
while (1) {
uint8_t *ptr;
res = ssl_read(ssl_sock, &ptr);
// printf("Received %d bytes\n\n", res);
if (res < 0) {
break;
}
if (res > 0) {
buffer_size += res;
buffer = realloc(buffer, buffer_size);
strcat(buffer, ptr);
}
}
ssl_free(ssl_sock);
ssl_ctx_free(ssl_ctx);
// printf("%s", buffer);
uint8_t *sbuffer = malloc(buffer_size);
uint8_t *inbuf = buffer;
size_t inbytesleft = buffer_size;
uint8_t *outbuf = sbuffer;
size_t outbytesleft = buffer_size;
do {
int res = iconv_u2s((char **)&inbuf, &inbytesleft, (char **)&outbuf, &outbytesleft);
if (res < 0) {
inbuf++;
inbytesleft--;
}
} while (inbuf < buffer + buffer_size);
*outbuf = 0;
char *html_body = extract_html_body((char *)sbuffer);
parse_html(html_body);
printf("%s", html_body);
free(buffer);
free(sbuffer);
return 0;
}