Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

fo_threadlookup.c

Go to the documentation of this file.
00001 
00011 /* {{{ Initial comments */
00012 /*
00013  * $LastChangedDate$
00014  * $LastChangedRevision$
00015  * $LastChangedBy$
00016  *
00017  */
00018 /* }}} */
00019 
00020 /* {{{ Includes */
00021 #include "config.h"
00022 #include "defines.h"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <dlfcn.h>
00027 #include <ctype.h>
00028 
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 
00032 #include <dirent.h>
00033 
00034 /* socket includes */
00035 #include <sys/socket.h>
00036 #include <netdb.h>
00037 #include <unistd.h>
00038 #include <sys/un.h>
00039 
00040 #include "hashlib.h"
00041 #include "utils.h"
00042 #include "configparser.h"
00043 #include "cfcgi.h"
00044 #include "template.h"
00045 #include "readline.h"
00046 #include "clientlib.h"
00047 #include "fo_tid_index.h"
00048 /* }}} */
00049 
00050 /* {{{ is_tid */
00056 int is_tid(const u_char *c) {
00057   register u_char *ptr = (u_char *)c;
00058 
00059   for(;*ptr;ptr++) {
00060     if(!isdigit(*ptr)) return -1;
00061   }
00062 
00063   return 0;
00064 }
00065 /* }}} */
00066 
00067 /* {{{ cmp */
00068 #ifndef DOXYGEN
00069 int cmp(const void *elem1,const void *elem2) {
00070   u_int64_t *tid = (u_int64_t *)elem1;
00071   t_tid_index *id = (t_tid_index *)elem2;
00072 
00073   if(*tid >= id->start && *tid <= id->end) return 0;
00074   if(*tid < id->start) return -1;
00075 
00076   return 1;
00077 }
00078 #endif
00079 /* }}} */
00080 
00081 /* {{{ main */
00089 int main(int argc,char *argv[],char *envp[]) {
00090   t_array *cfgfiles;
00091   u_char *file;
00092   t_configfile dconf;
00093   u_int64_t tid;
00094   u_char *ctid;
00095   t_array index;
00096   struct stat st;
00097   t_name_value *v;
00098   FILE *fd;
00099   t_tid_index *idx;
00100   t_name_value *archive_path;
00101   u_char *port = getenv("SERVER_PORT");
00102   t_array infos;
00103   size_t i;
00104 
00105   static const u_char *wanted[] = {
00106     "fo_default"
00107   };
00108 
00109   cf_cgi_parse_path_info(&infos);
00110 
00111   if((cfgfiles = get_conf_file(wanted,1)) == NULL) {
00112     fprintf(stderr,"error getting config files\n");
00113     printf("Status: 404 Not Found\015\012");
00114     return EXIT_FAILURE;
00115   }
00116 
00117   file = array_element_at(cfgfiles,0);
00118   cfg_init_file(&dconf,file);
00119   cfg_register_options(&dconf,default_options);
00120   free(file);
00121 
00122   if(read_config(&dconf,NULL) != 0) {
00123     fprintf(stderr,"config file error!\n");
00124 
00125     cfg_cleanup_file(&dconf);
00126 
00127     printf("Status: 404 Not Found\015\012");
00128     return EXIT_FAILURE;
00129   }
00130 
00131   if(infos.elements != 2 && infos.elements != 4) {
00132     printf("Status: 404 Not Found\015\012");
00133     return EXIT_FAILURE;
00134   }
00135 
00136   ctid = *((char **)array_element_at(&infos,1));
00137   if(is_tid(ctid) == -1) {
00138     printf("Status: 404 Not Found\015\012");
00139     return EXIT_FAILURE;
00140   }
00141   if((v = cfg_get_value(&fo_default_conf,"ThreadIndexFile")) == NULL) {
00142     printf("Status: 404 Not Found\015\012");
00143     return EXIT_FAILURE;
00144   }
00145   if((archive_path = cfg_get_value(&fo_default_conf,"ArchiveURL")) == NULL) {
00146     printf("Status: 404 Not Found\015\012");
00147     return EXIT_FAILURE;
00148   }
00149   if(stat(v->values[0],&st) == -1) {
00150     printf("Status: 404 Not Found\015\012");
00151     return EXIT_FAILURE;
00152   }
00153 
00154   tid = strtoull(ctid,NULL,10);
00155 
00156   /* do a lookup */
00157   array_init(&index,sizeof(t_tid_index),NULL);
00158 
00159   index.array    = fo_alloc(NULL,1,st.st_size,FO_ALLOC_MALLOC);
00160   index.reserved = st.st_size;
00161   index.elements = st.st_size / sizeof(t_tid_index);
00162 
00163   if((fd = fopen(v->values[0],"r")) == NULL) {
00164     printf("Status: 404 Not Found\015\012");
00165     return EXIT_FAILURE;
00166   }
00167   fread(index.array,sizeof(t_tid_index),st.st_size/sizeof(t_tid_index),fd);
00168   fclose(fd);
00169 
00170   if((idx = array_bsearch(&index,(void *)&tid,cmp)) == NULL) {
00171     printf("Status: 404 Not Found\015\012");
00172     return EXIT_FAILURE;
00173   }
00174 
00175   printf("Status: 302 Moved Temporarily\015\012");
00176 
00177 
00178   if(!port || cf_strcmp(port,"80") == 0) {
00179     if(infos.elements == 4) {
00180       printf("Location: http://%s%s%d/%d/%s/#m%s\015\012\015\012",getenv("SERVER_NAME"),archive_path->values[0],idx->year,idx->month,ctid,*((char **)array_element_at(&infos,3)));
00181     }
00182     else {
00183       printf("Location: http://%s%s%d/%d/%s/\015\012\015\012",getenv("SERVER_NAME"),archive_path->values[0],idx->year,idx->month,ctid);
00184     }
00185   }
00186   else {
00187     if(infos.elements == 4) {
00188       printf("Location: http://%s:%s%s%d/%d/%s/#m%s\015\012\015\012",getenv("SERVER_NAME"),getenv("SERVER_PORT"),archive_path->values[0],idx->year,idx->month,ctid,*((char **)array_element_at(&infos,3)));
00189     }
00190     else {
00191       printf("Location: http://%s:%s%s%d/%d/%s/\015\012\015\012",getenv("SERVER_NAME"),getenv("SERVER_PORT"),archive_path->values[0],idx->year,idx->month,ctid);
00192     }
00193   }
00194 
00195   cfg_cleanup(&fo_default_conf);
00196   cfg_cleanup_file(&dconf);
00197 
00198   array_destroy(cfgfiles);
00199   free(cfgfiles);
00200 
00201   return EXIT_SUCCESS;
00202 }
00203 /* }}} */
00204 
00205 /* eof */

Generated on Sun Apr 25 16:37:39 2004 for Classic Forum by doxygen 1.3.5