3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 # include <sys/stat.h>
30 # include <sys/types.h>
31 # include <sys/param.h>
42 } __attribute__ ((__packed__
)) dirent_t
;
44 #define VFS_FILEATTR_FILE 0x1
46 typedef struct web_context
{
47 struct web_context
*next
, *prev
;
56 web_t
*web_find (char *name
)
59 for (web
= web_list
.next
; web
!= &web_list
; web
= web
->next
)
60 if (!strcmp (web
->name
, name
))
66 web_t
*web_register (char *name
)
68 unsigned page_len
= 0;
70 char *page
= (char *) malloc (sizeof (char) * 80);
77 Content-Type: text/html
82 memcpy (page
, "HTTP/1.0 200 OK\n", 16);
83 memcpy (page
+16, "Content-Type: text/html\n", 24);
84 memcpy (page
+40, "Content-Length: ", 16);
85 memcpy (page
+56, " ", 5);
91 FILE *f
= fopen (name
, "r");
94 printf ("error -> file '%s' not found\n", name
);
98 fseek (f
, 0, SEEK_END
);
99 unsigned flen
= ftell (f
);
100 fseek (f
, 0, SEEK_SET
);
102 page
= (char *) realloc ((void *) page
, sizeof (char) * (flen
+ 80));
107 fread (page
+page_len
, flen
, 1, f
);
111 page
[page_len
] = '\0';
115 sprintf (num
, "%d", page_len
-63); // convert int to char *
116 unsigned num_len
= strlen (num
);
118 memcpy (page
+56, num
, num_len
);
122 web_t
*web
= (web_t
*) malloc (sizeof (web_t
));
127 web
->name
= strdup (name
);
129 web
->page_len
= page_len
;
132 web
->next
= &web_list
;
133 web
->prev
= web_list
.prev
;
134 web
->prev
->next
= web
;
135 web
->next
->prev
= web
;
137 printf ("> adding to cache: '%s'\n", name
);
150 : "=g" (dirent
) :: "%eax");
155 int http_loadfiles ()
158 dirent_t
*dir
= getdir ();
165 for (id
= 0; dir
[id
].next
; id
++) {
166 if (dir
[id
].attrib
& VFS_FILEATTR_FILE
) {
167 if (strstr (dir
[id
].name
, ".htm"))
168 if (!web_register (dir
[id
].name
))
173 struct dirent
*entry
; /* Only used as a pointer - see dirent.h */
174 struct stat statbuf
; /* So we can check which files are directories. */
175 char *pathname
= ".";
177 /* Open a directory for directory reads. */
178 DIR *directory
= opendir (pathname
);
181 printf ("ERROR -> cannot open %s\n", pathname
);
185 while ((entry
= readdir (directory
))) {
186 /* Skip if . or .. */
187 if (!strcmp (entry
->d_name
, ".") || !strcmp (entry
->d_name
, ".."))
190 /* stat it to see if it is a regular file. */
191 int retval
= stat (entry
->d_name
, &statbuf
);
193 if (retval
== 0 && statbuf
.st_mode
& S_IFREG
)
194 if (strstr (entry
->d_name
, ".htm"))
195 if (!web_register (entry
->d_name
))
200 closedir (directory
);
208 web_list
.next
= &web_list
;
209 web_list
.prev
= &web_list
;
211 if (!http_loadfiles ())
217 int http_transfer (client_t
*c
)
219 web_t
*web
= web_find (strlen (c
->page
) == 0 ? "i.htm" : c
->page
);
222 printf ("> sending '%s' web page\n", web
->name
);
224 send_to_socket (c
->fd
, web
->page
, web
->page_len
);
226 printf ("> web page '%s' not found\n", c
->page
);
228 c
->state
= CLIENT_STATE_DONE
;
236 for (web
= web_list
.next
; web
!= &web_list
; web
= web
->next
)