static gboolean bod_open(bod_state *state) { if (NULL == state->tempfile) { gint fd; GString *tmpfilename; const char tmpl[] = "lighttpd-buffer-XXXXXX", basedir[] = "/var/tmp"; tmpfilename = g_string_sized_new((sizeof(basedir) - 1) + 1 + (sizeof(tmpl) - 1)); g_string_append_len(tmpfilename, CONST_STR_LEN(basedir)); /* TODO: add config option */ li_path_append_slash(tmpfilename); g_string_append_len(tmpfilename, CONST_STR_LEN(tmpl)); fd = g_mkstemp(tmpfilename->str); if (-1 == fd) { VR_ERROR(state->vr, "g_mkstemp failed: %s", g_strerror(errno)); g_string_free(tmpfilename, TRUE); bod_stop(state); return FALSE; } state->tempfile = li_chunkfile_new(tmpfilename, fd, TRUE); state->write_pos = 0; state->flush_pos = 0; g_string_free(tmpfilename, TRUE); } return TRUE; }
/** uses/modifies wrk->tmp_str */ static void try_append_file(liVRequest *vr, GString **curbuf, const gchar *filename, gboolean encode_html) { GString *f = vr->wrk->tmp_str; g_string_truncate(f, 0); g_string_append_len(f, GSTR_LEN(vr->physical.path)); li_path_append_slash(f); g_string_append(f, filename); if (!encode_html) { int fd; struct stat st; while (-1 == (fd = open(f->str, O_RDONLY))) { if (errno == EINTR) continue; return; /* failed to open, ignore */ } if (-1 == fstat(fd, &st)) { close(fd); return; /* failed to open, ignore */ } if (st.st_size > MAX_INCLUDE_FILE_SIZE) { close(fd); return; /* file too big, ignore */ } /* flush current buffer and append file */ li_chunkqueue_append_string(vr->direct_out, *curbuf); *curbuf = g_string_sized_new(4*1024-1); li_chunkqueue_append_file_fd(vr->direct_out, NULL, 0, st.st_size, fd); } else { GError *error = NULL; gchar *contents; gsize length; if (!g_file_get_contents(f->str, &contents, &length, &error)) { g_error_free(error); return; /* ignore errors */ } if (length > MAX_INCLUDE_FILE_SIZE) { g_free(contents); return; /* file too big, ignore */ } g_string_append_len(*curbuf, CONST_STR_LEN("<pre>")); li_string_encode_append(contents, *curbuf, LI_ENCODING_HTML); g_string_append_len(*curbuf, CONST_STR_LEN("</pre>")); g_free(contents); } }