+ nm = req->rest;
+ path = sstrdup(".");
+ p = nm;
+ while(1) {
+ if((p2 = strchr(p, '/')) == NULL) {
+ } else {
+ *(p2++) = 0;
+ }
+
+ if(!*p) {
+ if(p2 == NULL) {
+ if(stat(path, &sb)) {
+ flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
+ goto fail;
+ }
+ break;
+ } else {
+ goto fail;
+ }
+ }
+ if(*p == '.')
+ goto fail;
+
+ getconfig(path);
+
+ /*
+ * First, check the name verbatimely:
+ */
+ buf = sprintf3("%s/%s", path, p);
+ if(!stat(buf, &sb)) {
+ if(S_ISDIR(sb.st_mode)) {
+ tmp = path;
+ if(!strcmp(path, "."))
+ path = sstrdup(p);
+ else
+ path = sprintf2("%s/%s", path, p);
+ free(tmp);
+ if(checkdir(req, fd, path))
+ break;
+ goto next;
+ }
+ if(S_ISREG(sb.st_mode)) {
+ tmp = path;
+ path = sprintf2("%s/%s", path, p);
+ free(tmp);
+ break;
+ }
+ goto fail;
+ }
+
+ /*
+ * Check the file extensionlessly:
+ */
+ if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
+ while((dent = readdir(dir)) != NULL) {
+ buf = sprintf3("%s/%s", path, dent->d_name);
+ if((p3 = strchr(dent->d_name, '.')) != NULL)
+ *p3 = 0;
+ if(strcmp(dent->d_name, p))
+ continue;
+ if(stat(buf, &sb))
+ continue;
+ if(!S_ISREG(sb.st_mode))
+ continue;
+ tmp = path;
+ path = sstrdup(buf);
+ free(tmp);
+ break;
+ }
+ closedir(dir);
+ if(dent != NULL)
+ break;
+ }
+
+ goto fail;
+
+ next:
+ if(p2 == NULL)
+ break;
+ p = p2;
+ }
+ if(p2 == NULL)
+ replrest(req, "");
+ else
+ replrest(req, p2);
+ if(!strncmp(path, "./", 2))
+ memmove(path, path + 2, strlen(path + 2) + 1);
+ if(S_ISDIR(sb.st_mode)) {
+ handledir(req, fd, path);
+ } else if(S_ISREG(sb.st_mode)) {
+ handlefile(req, fd, path);
+ } else {
+ goto fail;
+ }
+ goto out;
+
+fail:
+ /* XXX: Send error report. */
+out:
+ free(path);