Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] ability to use css #31

Open
ITwrx opened this issue Feb 26, 2023 · 7 comments
Open

[FR] ability to use css #31

ITwrx opened this issue Feb 26, 2023 · 7 comments

Comments

@ITwrx
Copy link

ITwrx commented Feb 26, 2023

It sure would be nice if i could specify a external style sheet with a --css option or maybe <link rel="stylesheet" href="darkhttpd.css"> could just be hardcoded in the source along with an empty (or basic) darkhttpd.css style sheet also included that could be modified by the user, if desired. I would like to specify body background, font size, colors, etc.

I saw a couple places in the darkhttpd source it looks like it might could "slide on in", but i don't know C, so i'm refraining from submitting a bad guess PR. :)

thanks

@ITwrx ITwrx changed the title ability to use css [FR] ability to use css Feb 26, 2023
@guest271314
Copy link

CSS will load with <link rel="stylesheet" href="darkhttpd.css"> and darkhttpd.css in the same directory.

@ITwrx
Copy link
Author

ITwrx commented Mar 19, 2023

@guest271314 I guess i should have specified that i mean for the html that darkhttpd generates for serving whatever files happen to be in a directory, not serving my theoretical custom static html files. I'm using it for an alpine linux mirror, for example. i don't have any html it's serving. My understanding is that darkhttpd is generating it's own basic html to show the directories and package tarballs, and this FR asks that the <link rel="stylesheet" href="darkhttpd.css"> be added to that darkhttpd generation source code or a --css flag added.

@guest271314
Copy link

I guess i should have specified that i mean for the html that darkhttpd generates for serving whatever files happen to be in a directory

I'm not familiar with that option.

I was just reading the source code at https://github.com/emikulic/darkhttpd/blob/master/darkhttpd.c#L1530-L1579 however I don't see a way to serve that by passing options.

@guest271314
Copy link

It looks like this is where the directory listing is generated

darkhttpd/darkhttpd.c

Lines 1966 to 2056 in 11d36de

static void generate_dir_listing(struct connection *conn, const char *path,
const char *decoded_url) {
char date[DATE_LEN], *spaces;
struct dlent **list;
ssize_t listsize;
size_t maxlen = 2; /* There has to be ".." */
int i;
struct apbuf *listing;
listsize = make_sorted_dirlist(path, &list);
if (listsize == -1) {
default_reply(conn, 500, "Internal Server Error",
"Couldn't list directory: %s", strerror(errno));
return;
}
for (i=0; i<listsize; i++) {
size_t tmp = strlen(list[i]->name);
if (maxlen < tmp)
maxlen = tmp;
}
listing = make_apbuf();
append(listing, "<html>\n<head>\n<title>");
append_escaped(listing, decoded_url);
append(listing,
"</title>\n"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
"</head>\n<body>\n<h1>");
append_escaped(listing, decoded_url);
append(listing, "</h1>\n<tt><pre>\n");
spaces = xmalloc(maxlen);
memset(spaces, ' ', maxlen);
for (i=0; i<listsize; i++) {
/* If a filename is made up of entirely unsafe chars,
* the url would be three times its original length.
*/
char safe_url[MAXNAMLEN*3 + 1];
urlencode(list[i]->name, safe_url);
append(listing, "<a href=\"");
append(listing, safe_url);
if (list[i]->is_dir)
append(listing, "/");
append(listing, "\">");
append_escaped(listing, list[i]->name);
append(listing, "</a>");
if (list[i]->is_dir)
append(listing, "/\n");
else {
appendl(listing, spaces, maxlen-strlen(list[i]->name));
appendf(listing, "%10llu\n", llu(list[i]->size));
}
}
cleanup_sorted_dirlist(list, listsize);
free(list);
free(spaces);
append(listing,
"</pre></tt>\n"
"<hr>\n");
rfc1123_date(date, now);
append(listing, generated_on(date));
append(listing, "</body>\n</html>\n");
conn->reply = listing->str;
conn->reply_length = (off_t)listing->length;
free(listing); /* don't free inside of listing */
conn->header_length = xasprintf(&(conn->header),
"HTTP/1.1 200 OK\r\n"
"Date: %s\r\n"
"%s" /* server */
"Accept-Ranges: bytes\r\n"
"%s" /* keep-alive */
"%s" /* custom headers */
"Content-Length: %llu\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n",
date, server_hdr, keep_alive(conn), custom_hdrs,
llu(conn->reply_length));
conn->reply_type = REPLY_GENERATED;
conn->http_code = 200;
}
. In theory you should be able to include the CSS in the source code that generates the HTML in a <style> element, then compile darkhttpd.

@ITwrx
Copy link
Author

ITwrx commented Mar 19, 2023

i would rather the <link rel="stylesheet" href="darkhttpd.css"> be included in darkhttpd source, so that each user can just read the docs and add an optional darkhttpd.css. That's why i submitted an FR instead of just modifying my copy and moving on, assuming i could do it correctly since i don't write c. :)
thanks

@emikulic
Copy link
Owner

emikulic commented Mar 20, 2023

You probably want href="/absolute/path.css" so that it also works from nested directories and not just the root.

This is starting to get into "darkhttpd is not intended to be a production webserver" territory though. Directory listings are meant to be quick and dirty so you can download one of a small number of files in a hurry. If you want stylized ones, at some point you should make your own and save them as a set of index.html files.

@ITwrx
Copy link
Author

ITwrx commented Mar 20, 2023

sure, my <link> is really just an example to explain the FR. I know dir listings and the auto-generated html is supposed to be minimalist, and i can get behind that, but i can't predict these dirs, as they are pulled in from rsync'd remote source and just being able to control background color, font size, etc of the generated html would make a huge difference, with only the <link> being added to darkhttpd source. I think users would like it, but it's up to you of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants