diff --git a/src/lib/uri.c b/src/lib/uri.c index f8c61c1..cb20bec 100644 --- a/src/lib/uri.c +++ b/src/lib/uri.c @@ -39,7 +39,7 @@ int path_exists(const char *path) { } int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) { - char buf0[1024], buf1[1024], buf2[1024], buf3[1024]; + char buf0[1024], buf1[1024], buf2[1024], buf3[1024], buf4[1024]; int p_len; uri->webroot = NULL; @@ -107,11 +107,11 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo while (1) { sprintf(buf0, "%s%s", uri->webroot, uri->path); - p_len = snprintf(buf1, sizeof(buf1), "%s.php", buf0); + p_len = snprintf(buf1, sizeof(buf1), "%s.xhtml", buf0); if (p_len < 0 || p_len >= sizeof(buf1)) return -1; p_len = snprintf(buf2, sizeof(buf2), "%s.html", buf0); if (p_len < 0 || p_len >= sizeof(buf2)) return -1; - p_len = snprintf(buf3, sizeof(buf3), "%s.xhtml", buf0); + p_len = snprintf(buf3, sizeof(buf3), "%s.php", buf0); if (p_len < 0 || p_len >= sizeof(buf3)) return -1; if (strlen(uri->path) <= 1 || path_exists(buf0) || path_is_file(buf1) || path_is_file(buf2) || path_is_file(buf3)) @@ -121,29 +121,28 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo parent_dir: ptr = strrchr(uri->path, '/'); size = (long) strlen(ptr); - sprintf(buf3, "%.*s%s", (int) size, ptr, uri->pathinfo); - strcpy(uri->pathinfo, buf3); + sprintf(buf4, "%.*s%s", (int) size, ptr, uri->pathinfo); + strcpy(uri->pathinfo, buf4); ptr[0] = 0; } if (uri->pathinfo[0] != 0) { - sprintf(buf3, "%s", uri->pathinfo + 1); - strcpy(uri->pathinfo, buf3); + sprintf(buf4, "%s", uri->pathinfo + 1); + strcpy(uri->pathinfo, buf4); } if (path_is_file(buf0)) { uri->filename = malloc(strlen(buf0) + 1); strcpy(uri->filename, buf0); long len = (long) strlen(uri->path); - if (strends(uri->path, ".php")) { - uri->path[len - 4] = 0; - uri->is_static = 0; + if (strends(uri->path, ".xhtml")) { + uri->path[len - 6] = 0; } else if (strends(uri->path, ".html")) { uri->path[len - 5] = 0; - } else if (strends(uri->path, ".xhtml")) { - uri->path[len - 6] = 0; + } else if (strends(uri->path, ".php")) { + uri->path[len - 4] = 0; + uri->is_static = 0; } } else if (path_is_file(buf1)) { - uri->is_static = 0; uri->filename = malloc(strlen(buf1) + 1); strcpy(uri->filename, buf1); } else if (path_is_file(buf2)) { @@ -152,22 +151,27 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo } else if (path_is_file(buf3)) { uri->filename = malloc(strlen(buf3) + 1); strcpy(uri->filename, buf3); + uri->is_static = 0; } else { uri->is_dir = 1; strcpy(uri->path + strlen(uri->path), "/"); - sprintf(buf1, "%s%s" "index.php", uri->webroot, uri->path); + sprintf(buf1, "%s%s" "index.xhtml", uri->webroot, uri->path); sprintf(buf2, "%s%s" "index.html", uri->webroot, uri->path); - sprintf(buf3, "%s%s" "index.xhtml", uri->webroot, uri->path); - if (path_is_file(buf1)) { + sprintf(buf3, "%s%s" "index.php", uri->webroot, uri->path); + if (path_is_file(buf3) && uri->pathinfo[0] != 0) { + uri->filename = malloc(strlen(buf3) + 1); + strcpy(uri->filename, buf3); + uri->is_static = 0; + } else if (path_is_file(buf1)) { uri->filename = malloc(strlen(buf1) + 1); strcpy(uri->filename, buf1); - uri->is_static = 0; } else if (path_is_file(buf2)) { uri->filename = malloc(strlen(buf2) + 1); strcpy(uri->filename, buf2); } else if (path_is_file(buf3)) { uri->filename = malloc(strlen(buf3) + 1); strcpy(uri->filename, buf3); + uri->is_static = 0; } else { if (dir_mode == URI_DIR_MODE_FORBIDDEN) { uri->is_static = 1; @@ -184,17 +188,17 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo } } - if (strends(uri->path + strlen(uri->path), "index")) + if (strends(uri->path, "/index")) uri->path[strlen(uri->path) - 5] = 0; if (streq(uri->pathinfo, "index.php") || streq(uri->pathinfo, "index.html") || streq(uri->pathinfo, "index.xhtml")) uri->pathinfo[0] = 0; - sprintf(buf0, "%s%s%s%s%s", uri->path, + sprintf(buf4, "%s%s%s%s%s", uri->path, (strlen(uri->pathinfo) == 0 || uri->path[strlen(uri->path) - 1] == '/') ? "" : "/", uri->pathinfo, uri->query != NULL ? "?" : "", uri->query != NULL ? uri->query : ""); - uri->uri = malloc(strlen(buf0) + 1); - strcpy(uri->uri, buf0); + uri->uri = malloc(strlen(buf4) + 1); + strcpy(uri->uri, buf4); return 0; }