static char * wiki_get_pagename (char * dir) { char * name; char * ch; /* get URI and decode it to ISO-8859-1 */ name = request_get_uri(server); /* get rid of file extension directory and the 2 slashes around */ name += strlen(dir); /* get rid of extension itself */ ch = name; while (*ch && (*ch != '.')) ch++; *ch = '\0'; return name; }
/* * Here we send a disk file to the client, if the file exists. * A few rules: * a) The URI must be valid, no .. * b) The documentroot will be prepended to the file. No empty * documentroots allowed. Minimum docroot is 2 characters, and * it cannot contain "..". The docroot must either be ./ or / or * something longer. */ static int send_disk_file( http_server s, connection conn, http_request req, http_response response, meta_error e) { size_t i; const char *uri, *docroot; char filename[CCH_URI_MAX + DOCUMENTROOT_MAX + 2]; struct stat st; page_attribute a; http_version v; const char* content_type; assert(s != NULL); assert(conn != NULL); assert(req != NULL); assert(response != NULL); assert(e != NULL); (void)conn; /* TODO: Sjekk hvorfor funksjonen ikke sender fila??? */ /* We need a valid uri */ if( (uri = request_get_uri(req)) == NULL || strlen(uri) == 0 || strstr(uri, "..")) return set_http_error(e, HTTP_400_BAD_REQUEST); /* We need a valid documentroot */ if( (docroot = http_server_get_documentroot(s)) == NULL) return set_http_error(e, HTTP_400_BAD_REQUEST); i = strlen(docroot); if (i < 1 || (i == 1 && *docroot != '/') || (i == 2 && strcmp(docroot, "./") != 0) || (docroot[0] == '.' && docroot[1] == '.') || strstr(docroot, "..") != NULL ) return set_http_error(e, HTTP_400_BAD_REQUEST); /* We need space for the absolute path */ i += strlen(uri) + 2; if(i >= sizeof(filename)) return set_http_error(e, HTTP_400_BAD_REQUEST); /* Create the path */ sprintf(filename, "%s/%s", docroot, uri); /* Does the file exist? */ if(stat(filename, &st)) return set_http_error(e, HTTP_404_NOT_FOUND); else if(S_ISREG(st.st_mode)) ; else if(S_ISDIR(st.st_mode)) { strcat(filename, "/index.html"); if(stat(filename, &st)) return set_http_error(e, HTTP_404_NOT_FOUND); else if(!S_ISREG(st.st_mode)) return set_http_error(e, HTTP_400_BAD_REQUEST); } else return set_http_error(e, HTTP_400_BAD_REQUEST); /* We must check page_attributes even for files loaded from disk. */ /* NOTE: Trengs dette for HTTP 1.0 ?*/ v = request_get_version(req); if( (a = http_server_get_default_attributes(s)) != NULL && !check_attributes(req, a)) { response_set_status(response, HTTP_406_NOT_ACCEPTABLE); return set_http_error(e, HTTP_406_NOT_ACCEPTABLE); } content_type = get_mime_type(filename); /* This function does not actually send the file, just stats it * and stores the path. The contents will be sent later when * response_send_entity is called. */ if(!response_send_file(response, filename, content_type, e)) return 0; else { response_set_status(response, HTTP_200_OK); return 1; } }