Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / webcl / http.c
blobb6b4e762fd5bc8b2eea6c876852d173d5356fe7a
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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 #ifndef LINUX
38 setcolor (color_font, color_bg);
39 #endif
42 int http_proto (char *data, int len)
44 /*data[len] = '\0';
45 puts ("PROTO: '");
46 puts (data);
47 puts ("' - ");
49 itoa (len, num, 10);
50 puts (num);
51 puts ("\n");*/
53 if (!strncmp (data, "html", len)) {
54 printf ("<WEB PAGE>\n");
55 return 1;
58 if (!strncmp (data, "/html", len)) {
59 printf ("\n<END OF WEB>\n");
60 return 1;
63 if (!strncmp (data, "br", len)) {
64 putchar ('\n');
65 return 1;
68 if (!strncmp (data, "h1", len)) {
69 color_bg = 2;
70 color_apply ();
71 return 1;
74 if (!strncmp (data, "/h1", len)) {
75 color_bg = 8;
76 color_apply ();
77 return 1;
80 if (!strncmp (data, "p", len)) {
81 putchar ('|');
82 return 1;
85 if (!strncmp (data, "/p", len)) {
86 putchar ('|');
87 return 1;
90 if (!strncmp (data, "hr", len)) {
91 putchar ('\n');
93 unsigned i = 0;
94 while (i < 80) {
95 putchar (205);
96 i ++;
99 putchar ('\n');
100 return 1;
103 if (!strncmp (data, "a href", 6)) {
104 color_font = 8;
105 color_apply ();
106 return 1;
109 if (!strncmp (data, "/a", len)) {
110 color_font = 15;
111 color_apply ();
112 return 1;
115 return 0;
118 int http_handler (char *data, int len)
120 int i = 0;
121 unsigned tag_s = 0;
122 unsigned tag_e = 0;
123 unsigned l = 0;
126 while (i < len) {
127 /* start of html tag */
128 if (data[i] == '<')
129 tag_s = i;
131 if (data[i] == '>') {
132 tag_e = i;
133 l = tag_s;
136 if (!(tag_s && tag_e)) {
137 if (l > tag_s)
138 putchar (data[i]);
141 if (tag_e > tag_s) {
142 if (tag_s && tag_e) {
143 http_proto (data+tag_s+1, tag_e-tag_s-1);
144 tag_s = 0;
145 tag_e = 0;
149 i ++;
152 return 0;
155 int http_request (char *address, unsigned address_len, char *dir, unsigned dir_len)
157 /*GET / HTTP/1.1
158 Host: 127.0.0.1
159 Accept: text/xml,text/html;text/plain
160 Accept-Charset: ISO-8859-2,utf-8
161 Keep-Alive: 300
162 Connection: keep-alive*/
164 char *buf = (char *) malloc (sizeof (char) * 200240); // Read buffer
165 //char buf[10240];
167 memcpy (buf, "GET ", 4);
168 memcpy (buf+4, dir, dir_len);
169 memcpy (buf+4+dir_len, " HTTP/1.1\r\nHost: www.", 21);
170 memcpy (buf+25+dir_len, address, address_len);
171 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);
173 unsigned len = 117+dir_len+address_len;
174 buf[len] = '\0';
176 int ret = send (sock, buf, len, 0);
177 int buf_len = 0;
179 /* receive header */
180 while (1) {
181 ret = recv (sock, buf+buf_len, 1024, 0);
182 printf ("ret: %d : %d\n", ret, buf_len);
183 if (ret < 1)
184 break;
185 else
186 buf_len += ret;
188 if (buf_len >= 200240) {
189 printf ("WEB page is too big\n");
190 goto disconnect;
194 len = 0;
196 if (buf_len > 0) {
197 buf[buf_len] = '\0';
199 char *r = strstr (buf, "Content-Length: ");
201 if (r) {
202 unsigned x = 0;
203 while (x < 7) {
204 if (r[x+16] == '\n')
205 break;
207 x ++;
210 char num[9];
211 memcpy (num, r+16, x);
212 num[x-1] = '\0';
214 len = atoi (num);
216 ret = 1;
218 if (!len) {
219 puts ("ERROR 404 - Page not found\n");
220 ret = 0;
221 goto disconnect;
223 } else {
224 int p;
225 for (p = 0; p < buf_len; p ++) {
226 if (buf[p] == '\n')
227 if (buf[p+1] == '\r')
228 /*if (buf[p+2] == '\n')
229 if (buf[p+3] == '\r') */{
230 len = buf_len - p;
231 break;
234 printf ("len: %d\n", len);
239 printf ("WEB PAGE is loaded\n");
241 color_font = 15;
242 color_bg = 0;
244 http_handler (buf+(buf_len-len), len);
246 disconnect:
247 free (buf);
248 close (sock);
250 return ret;