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