System call sys_getchar () was improved; stdin is correspond with 0. fd and stdout...
[ZeXOS.git] / apps / webcl / http.c
blob3a4acbfd0e4ef2921be5a690e66b9d4a42cea14e
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/socket.h>
28 extern int sock;
30 char num[20];
32 unsigned char color_font;
33 unsigned char color_bg;
35 void color_apply ()
37 setcolor (color_font, color_bg);
40 int http_proto (char *data, int len)
42 /*data[len] = '\0';
43 puts ("PROTO: '");
44 puts (data);
45 puts ("' - ");
47 itoa (len, num, 10);
48 puts (num);
49 puts ("\n");*/
51 if (!strncmp (data, "html", len)) {
52 printf ("<WEB PAGE>\n");
53 return 1;
56 if (!strncmp (data, "/html", len)) {
57 printf ("\n<END OF WEB>\n");
58 return 1;
61 if (!strncmp (data, "br", len)) {
62 putch ('\n');
63 return 1;
66 if (!strncmp (data, "h1", len)) {
67 color_bg = 2;
68 color_apply ();
69 return 1;
72 if (!strncmp (data, "/h1", len)) {
73 color_bg = 8;
74 color_apply ();
75 return 1;
78 if (!strncmp (data, "p", len)) {
79 putch ('|');
80 return 1;
83 if (!strncmp (data, "/p", len)) {
84 putch ('|');
85 return 1;
88 if (!strncmp (data, "hr", len)) {
89 putch ('\n');
91 unsigned i = 0;
92 while (i < 80) {
93 putch (205);
94 i ++;
97 putch ('\n');
98 return 1;
101 if (!strncmp (data, "a href", 6)) {
102 color_font = 8;
103 color_apply ();
104 return 1;
107 if (!strncmp (data, "/a", len)) {
108 color_font = 15;
109 color_apply ();
110 return 1;
113 return 0;
116 int http_handler (char *data, int len)
118 int i = 0;
119 unsigned tag_s = 0;
120 unsigned tag_e = 0;
121 unsigned l = 0;
124 while (i < len) {
125 /* start of html tag */
126 if (data[i] == '<')
127 tag_s = i;
129 if (data[i] == '>') {
130 tag_e = i;
131 l = tag_s;
134 if (!(tag_s && tag_e)) {
135 if (l > tag_s)
136 putch (data[i]);
139 if (tag_e > tag_s) {
140 if (tag_s && tag_e) {
141 http_proto (data+tag_s+1, tag_e-tag_s-1);
142 tag_s = 0;
143 tag_e = 0;
147 i ++;
150 return 0;
153 int http_request (char *address, unsigned address_len, char *dir, unsigned dir_len)
155 /*GET / HTTP/1.1
156 Host: 127.0.0.1
157 Accept: text/xml,text/html;text/plain
158 Accept-Charset: ISO-8859-2,utf-8
159 Keep-Alive: 300
160 Connection: keep-alive*/
162 char *buf = (char *) malloc (sizeof (char) * 10240); // Read buffer
163 //char buf[10240];
165 memcpy (buf, "GET ", 4);
166 memcpy (buf+4, dir, dir_len);
167 memcpy (buf+4+dir_len, " HTTP/1.1\r\nHost: www.", 21);
168 memcpy (buf+25+dir_len, address, address_len);
169 memcpy (buf+25+dir_len+address_len, "\r\nAccept: */*\r\nAccept-Charset: ISO-8859-2,utf-8\r\nKeep-Alive: 300\r\nConnection: Keep-Alive\r\n\r\n", 92);
171 unsigned len = 117+dir_len+address_len;
172 buf[len] = '\0';
174 int ret = send (sock, buf, len, 0);
175 int buf_len = 0;
177 /* receive header */
178 while (1) {
179 ret = recv (sock, buf+buf_len, 512, 0);
180 printf ("ret: %d\n", ret);
181 if (ret < 1)
182 break;
183 else
184 buf_len += ret;
186 if (buf_len >= 10240) {
187 printf ("WEB page is too big\n");
188 goto disconnect;
192 len = 0;
194 if (buf_len > 0) {
195 buf[buf_len] = '\0';
197 char str[buf_len+1];
198 memcpy (str, buf, buf_len);
199 str[buf_len] = '\0';
201 //puts (str);
203 char *r = strstr (str, "Content-Length: ");
205 if (buf_len) {
206 unsigned x = 0;
207 while (x < 7) {
208 if (r[x+16] == '\n')
209 break;
211 x ++;
214 char num[8];
215 memcpy (num, r+16, x);
216 num[x-1] = '\0';
218 len = atoi (num);
220 ret = 1;
222 if (!len) {
223 puts ("ERROR 404 - Page not found\n");
224 ret = 0;
225 goto disconnect;
231 printf ("WEB PAGE is loaded\n");
233 color_font = 15;
234 color_bg = 0;
236 http_handler (buf+(buf_len-len), len);
238 disconnect:
239 free (buf);
240 close (sock);
242 return ret;