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

Update http.c #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 55 additions & 13 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,38 +330,80 @@ http_callback_disconnect(httpd * webserver, request * r)
return;
}

void
send_http_page(request * r, const char *title, const char *message)
/** @brief http template page read only once
*/
static char*
get_http_page()
{
s_config *config = config_get_config();
char *buffer;
struct stat stat_info;
int fd;
ssize_t written;
s_config *config = config_get_config();

static struct {
char buff[10240];
int len;
int loaded;
} page = {
.len = 0,
.loaded = 0,
};

if(page.loaded) {
if((page.len > 0)) {
return safe_strdup(page.buff);
} else {
debug(LOG_CRIT, "HTML has loaded, but return size 0, maybe file %s can not find",
config->htmlmsgfile);
return NULL;
}
}

fd = open(config->htmlmsgfile, O_RDONLY);
if (fd == -1) {
debug(LOG_CRIT, "Failed to open HTML message file %s: %s", config->htmlmsgfile, strerror(errno));
return;
page.loaded = 1;
page.len= -1;

return NULL;
}

if (fstat(fd, &stat_info) == -1) {
debug(LOG_CRIT, "Failed to stat HTML message file: %s", strerror(errno));
close(fd);
return;

page.loaded = 1;
page.len= -1;

return NULL;
}
// Cast from long to unsigned int
buffer = (char *)safe_malloc((size_t) stat_info.st_size + 1);
written = read(fd, buffer, (size_t) stat_info.st_size);

written = read(fd, page.buff, (size_t) stat_info.st_size);
if (written == -1) {
debug(LOG_CRIT, "Failed to read HTML message file: %s", strerror(errno));
free(buffer);
close(fd);
return;

page.loaded = 1;
page.len= -1;
return NULL;
}

page.buff[written] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're also not checking for overflows - at that point, you might be corrupting memory if the file is too large.

page.len= written;

close(fd);
return safe_strdup(page.buff);
}

void
send_http_page(request * r, const char *title, const char *message)
{
// read http page from memory direct, only load once
char *buffer = get_http_page();
s_config *config = config_get_config();

if(!buffer)
return;

buffer[written] = 0;
httpdAddVariable(r, "title", title);
httpdAddVariable(r, "message", message);
httpdAddVariable(r, "nodeID", config->gw_id);
Expand Down