content type handling
[webgit] / src / webgit.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007,               Christian Thaeter <ct@pipapo.org>
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
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.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22
23 #include "webgit.h"
24
25 #define WEBGIT_CONFIG "./webgit.conf"
26
27 #ifndef WEBGIT_CONFIG
28 #define WEBGIT_CONFIG "/etc/webgit.conf"
29 #endif
30
31 #include "actions.h"
32 #include "query.h"
33 #include "options.h"
34
35 #include "git/git-compat-util.h"
36
37 #include <cwa.h>
38
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <time.h>
43 #include <setjmp.h>
44
45 Html error_log;
46 jmp_buf err_jmp;
47
48 void
49 webgit_err_vargs (const char *err, va_list params)
50 {
51   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag ("br")));
52   longjmp (err_jmp, 0);
53 }
54
55 void
56 webgit_warn_vargs (const char *err, va_list params)
57 {
58   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag("br")));
59 }
60
61
62 static void
63 webgit_setup (void)
64 {
65   set_die_routine (webgit_err_vargs);
66   set_error_routine (webgit_err_vargs);
67   set_warn_routine (webgit_warn_vargs);
68 }
69
70
71 Html
72 webgit_email_link (Html name, Html email)
73 {
74   return html (
75                html_tag("a",
76                         html_attr ("href",
77                                    html ("mailto:", email))
78                         ),
79                name
80                );
81 }
82
83
84 void
85 webgit_enter_repository (struct webgit_query* query)
86 {
87   LLIST_FOREACH (&query->repos, node)
88     {
89       struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
90       if (!strcmp (ri->name, query->repo))
91         {
92           if (chdir (ri->path))
93             return die ("error: wrong path %s", ri->path);
94           break;
95         }
96     }
97
98   setup_git_directory ();
99 }
100
101
102 const char*
103 webgit_mimetype (const char* name)
104 {
105   char buf[256];
106
107   char* ext = strrchr (name, '.');
108   if (!ext)
109     return "";
110   ++ext;
111   if (!*ext)
112     return "";
113
114   FILE* mime_types = fopen ("/etc/mime.types", "r");
115   if (!mime_types)
116     return "";
117
118   while (fgets (buf, 256, mime_types))
119     {
120       if (buf[0] == '#' || !strtok (buf, " \t\n"))
121         continue;
122
123       char* suffix;
124       while ((suffix = strtok (NULL, " \t\n")))
125         if (!strcmp (suffix, ext))
126           goto done;
127     }
128   *buf = '\0';
129
130  done:
131   fclose (mime_types);
132   return cwa_strndup (buf, SIZE_MAX);
133 }
134
135
136 int
137 main (int argc, char**argv)
138 {
139   Html page;
140   struct webgit_query query;
141
142   webgit_setup();
143
144   error_log = html_list();
145
146   clock_t timestat = clock();
147
148   webgit_query_init (&query);
149
150   /* set some defaults if not already set */
151   setenv ("REQUEST_METHOD", "GET", 0);
152   setenv ("SCRIPT_NAME", argv[0], 0);
153   setenv ("WEBGIT_CONFIG", WEBGIT_CONFIG, 0);
154
155   /* we can only longjmp out of libgit's error handling */
156   if (setjmp (err_jmp))
157     goto error;
158
159   if (webgit_commandline_dispatch (argc, argv, &query))
160     goto error;
161
162   // read config
163   if (webgit_configfile_dispatch (getenv("WEBGIT_CONFIG"), &query))
164     goto error;
165
166
167   // parse request/query What to show?
168
169   query.request = cgi_new ();
170
171   cgi_run_query (query.request, webgit_param_dispatch, &query);
172
173   /* default action is to show the main page with the list of repos */
174   if (!query.action)
175     query.action = cwa_strndup ("main", SIZE_MAX);
176
177   // query to cache-line
178
179   // check if in cache
180   
181   // generate page
182   
183   //Html page = webgit_action_dispatch (&query);
184
185   page = html(
186               html_httpheader(
187                               html_httpfield(
188                                              "Content-type",
189                                              query.content_type,
190                                              html_attr("charset", "UTF-8")
191                                              )//,
192                               //html_httpfield("Last-Modified", cgit_page.modified),
193                               //html_httpfield("Expires", cgit_page.expires)
194                               ),
195               html_document(
196                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
197                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
198                             html_content(
199                                          /*head*/
200                                          html(
201                                               html(html_tag("title"), "webgit"),
202                                               html_nl(),
203                                               html_meta("generator", "webgit")//,
204                                               //html_link_rel("stylesheet", "text/css", cgit_page.stylesheet)
205                                               ),
206                                          /*body*/
207                                          html (
208                                                html (
209                                                      html_tag("div"),
210                                                      webgit_action_dispatch (&query)
211                                                      ),
212                                                html(
213                                                     html_tag("div"),
214                                                     error_log
215                                                     )
216                                          ),
217                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
218                                          html_attr("xml:lang", "en"),
219                                          html_attr("lang", "en")
220                                          )
221                             )
222               );
223
224   timestat = clock() - timestat;
225   html_list_append (error_log, html (
226                                      html_fmt ("processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC),
227                                      html_tag("br")
228                                      )
229                     );
230
231   html_fprint (stdout, page);
232
233
234   html_free (page);
235
236   webgit_query_destroy (&query);
237
238   return 0;
239
240  error:
241   page = html(
242               html_httpheader(
243                               html_httpfield(
244                                              "Content-type",
245                                              "text/html",
246                                              html_attr("charset", "UTF-8")
247                                              )                              ),
248               html_document(
249                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
250                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
251                             html_content(
252                                          /*head*/
253                                          html(
254                                               html(html_tag("title"), "ERROR: webgit"),
255                                               html_nl(),
256                                               html_meta("generator", "webgit")
257                                               ),
258                                          /*body*/
259                                          html(
260                                               html_tag("div"),
261                                               error_log
262                                               ),
263                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
264                                          html_attr("xml:lang", "en"),
265                                          html_attr("lang", "en")
266                                          )
267                             )
268               );
269
270   html_fprint (stdout, page);
271   html_free (page);
272   return 10;
273 }