00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <u/libu.h>
00012 #include <klone/supplier.h>
00013 #include <klone/broker.h>
00014 #include <klone/request.h>
00015 #include <klone/http.h>
00016 #include "klone_conf.h"
00017
00018 enum { MAX_SUP_COUNT = 8 };
00019
00020 extern supplier_t sup_emb;
00021 #ifdef ENABLE_SUP_CGI
00022 extern supplier_t sup_cgi;
00023 #endif
00024 #ifdef ENABLE_SUP_FS
00025 extern supplier_t sup_fs;
00026 #endif
00027
00028 struct broker_s
00029 {
00030 supplier_t *sup_list[MAX_SUP_COUNT + 1];
00031 };
00032
00033 int broker_is_valid_uri(broker_t *b, http_t *h, request_t *rq, const char *buf,
00034 size_t len)
00035 {
00036 int i;
00037 time_t mtime;
00038
00039 dbg_goto_if (b == NULL, notfound);
00040 dbg_goto_if (buf == NULL, notfound);
00041
00042 for(i = 0; b->sup_list[i]; ++i)
00043 if(b->sup_list[i]->is_valid_uri(h, rq, buf, len, &mtime))
00044 return 1;
00045
00046 notfound:
00047 return 0;
00048 }
00049
00050 int broker_serve(broker_t *b, http_t *h, request_t *rq, response_t *rs)
00051 {
00052 const char *file_name;
00053 int i;
00054 time_t mtime, ims;
00055
00056 dbg_err_if (b == NULL);
00057 dbg_err_if (rq == NULL);
00058 dbg_err_if (rs == NULL);
00059
00060 file_name = request_get_resolved_filename(rq);
00061 for(i = 0; b->sup_list[i]; ++i)
00062 {
00063 if(b->sup_list[i]->is_valid_uri(h, rq, file_name, strlen(file_name),
00064 &mtime) )
00065 {
00066 ims = request_get_if_modified_since(rq);
00067 if(ims && ims >= mtime)
00068 {
00069 response_set_status(rs, HTTP_STATUS_NOT_MODIFIED);
00070 dbg_err_if(response_print_header(rs));
00071 } else {
00072 dbg_err_if(b->sup_list[i]->serve(rq, rs));
00073 if(response_get_status(rs) >= 400)
00074 return response_get_status(rs);
00075 }
00076
00077 return 0;
00078 }
00079 }
00080
00081 response_set_status(rs, HTTP_STATUS_NOT_FOUND);
00082 warn("404, file not found: %s", request_get_filename(rq));
00083
00084 err:
00085 return HTTP_STATUS_NOT_FOUND;
00086 }
00087
00088 int broker_create(broker_t **pb)
00089 {
00090 broker_t *b = NULL;
00091 int i;
00092
00093 dbg_err_if (pb == NULL);
00094
00095 b = u_zalloc(sizeof(broker_t));
00096 dbg_err_if(b == NULL);
00097
00098 i = 0;
00099 b->sup_list[i++] = &sup_emb;
00100
00101 #ifdef ENABLE_SUP_CGI
00102 b->sup_list[i++] = &sup_cgi;
00103 #else
00104 #ifndef OS_WIN
00105 warn("CGI support disabled, use --enable_cgi to enable it");
00106 #endif
00107 #endif
00108
00109 #ifdef ENABLE_SUP_FS
00110 b->sup_list[i++] = &sup_fs;
00111 #else
00112 warn("File system support disabled, use --enable_fs to enable it");
00113 #endif
00114
00115 b->sup_list[i++] = NULL;
00116
00117 for(i = 0; b->sup_list[i]; ++i)
00118 dbg_err_if(b->sup_list[i]->init());
00119
00120 *pb = b;
00121
00122 return 0;
00123 err:
00124 if(b)
00125 broker_free(b);
00126 return ~0;
00127 }
00128
00129 int broker_free(broker_t *b)
00130 {
00131 int i;
00132
00133 if (b)
00134 {
00135 for(i = 0; b->sup_list[i]; ++i)
00136 b->sup_list[i]->term();
00137
00138 U_FREE(b);
00139 }
00140
00141 return 0;
00142 }
00143