Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 11, 2024
1 parent 0f061fe commit 8c52c5e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 122 deletions.
63 changes: 23 additions & 40 deletions src/php_spx.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,29 +924,20 @@ static void http_ui_handler_shutdown(void)
ui_uri = "/index.html";
}

if (ui_uri[0] != '/') {
goto error_404;
}

if (0 == http_ui_handler_data(SPX_G(data_dir), ui_uri)) {
goto finish;
}

char local_file_absolute_path[PATH_MAX];

if (
spx_utils_resolve_confined_file_absolute_path(
SPX_G(http_ui_assets_dir),
ui_uri,
NULL,
local_file_absolute_path,
sizeof(local_file_absolute_path)
) == NULL
) {
goto error_404;
}
char local_file_name[512];
snprintf(
local_file_name,
sizeof(local_file_name),
"%s%s",
SPX_G(http_ui_assets_dir),
ui_uri
);

if (0 == http_ui_handler_output_file(local_file_absolute_path)) {
if (0 == http_ui_handler_output_file(local_file_name)) {
goto finish;
}

Expand Down Expand Up @@ -1034,34 +1025,26 @@ static int http_ui_handler_data(const char * data_dir, const char *relative_path

const char * get_report_metadata_uri = "/data/reports/metadata/";
if (spx_utils_str_starts_with(relative_path, get_report_metadata_uri)) {
char file_name[PATH_MAX];
if (
spx_reporter_full_build_metadata_file_name(
data_dir,
relative_path + strlen(get_report_metadata_uri) - 1,
file_name,
sizeof(file_name)
) == NULL
) {
return -1;
}
char file_name[512];
spx_reporter_full_build_metadata_file_name(
data_dir,
relative_path + strlen(get_report_metadata_uri),
file_name,
sizeof(file_name)
);

return http_ui_handler_output_file(file_name);
}

const char * get_report_uri = "/data/reports/get/";
if (spx_utils_str_starts_with(relative_path, get_report_uri)) {
char file_name[PATH_MAX];
if (
spx_reporter_full_build_file_name(
data_dir,
relative_path + strlen(get_report_uri) - 1,
file_name,
sizeof(file_name)
) == NULL
) {
return -1;
}
char file_name[512];
spx_reporter_full_build_file_name(
data_dir,
relative_path + strlen(get_report_uri),
file_name,
sizeof(file_name)
);

return http_ui_handler_output_file(file_name);
}
Expand Down
6 changes: 5 additions & 1 deletion src/php_spx.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
# error "Only the following PHP versions are supported: 5.4 to 8.3"
#endif

#if defined(ZTS) && !defined(CONTINUOUS_INTEGRATION)
# error "ZTS is not yet supported"
#endif

#define PHP_SPX_EXTNAME "SPX"
#define PHP_SPX_VERSION "0.4.16"
#define PHP_SPX_VERSION "0.4.15"

extern zend_module_entry spx_module_entry;
48 changes: 23 additions & 25 deletions src/spx_reporter_full.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ typedef struct {
typedef struct {
spx_profiler_reporter_t base;

char metadata_file_name[PATH_MAX];
char metadata_file_name[512];
metadata_t * metadata;
spx_output_stream_t * output;

Expand Down Expand Up @@ -99,7 +99,7 @@ size_t spx_reporter_full_metadata_list_files(
return 0;
}

char file_path[PATH_MAX];
char file_path[512];
size_t count = 0;
const struct dirent * entry;
while ((entry = readdir(dir)) != NULL) {
Expand All @@ -124,33 +124,33 @@ size_t spx_reporter_full_metadata_list_files(
return count;
}

char * spx_reporter_full_build_metadata_file_name(
int spx_reporter_full_build_metadata_file_name(
const char * data_dir,
const char * key,
char * file_name,
size_t size
) {
return spx_utils_resolve_confined_file_absolute_path(
data_dir,
key,
".json",
return snprintf(
file_name,
size
size,
"%s/%s.json",
data_dir,
key
);
}

char * spx_reporter_full_build_file_name(
int spx_reporter_full_build_file_name(
const char * data_dir,
const char * key,
char * file_name,
size_t size
) {
return spx_utils_resolve_confined_file_absolute_path(
data_dir,
key,
".txt.gz",
return snprintf(
file_name,
size
size,
"%s/%s.txt.gz",
data_dir,
key
);
}

Expand All @@ -173,21 +173,19 @@ spx_profiler_reporter_t * spx_reporter_full_create(const char * data_dir)
goto error;
}

char file_name[PATH_MAX];
snprintf(
file_name,
sizeof(file_name),
"%s/%s.txt.gz",
char file_name[512];
spx_reporter_full_build_file_name(
data_dir,
reporter->metadata->key
reporter->metadata->key,
file_name,
sizeof(file_name)
);

snprintf(
reporter->metadata_file_name,
sizeof(reporter->metadata_file_name),
"%s/%s.json",
spx_reporter_full_build_metadata_file_name(
data_dir,
reporter->metadata->key
reporter->metadata->key,
reporter->metadata_file_name,
sizeof(reporter->metadata_file_name)
);

(void) mkdir(data_dir, 0777);
Expand Down
2 changes: 1 addition & 1 deletion src/spx_reporter_full.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ size_t spx_reporter_full_metadata_list_files(
void (*callback) (const char *, size_t)
);

char * spx_reporter_full_build_metadata_file_name(
int spx_reporter_full_build_metadata_file_name(
const char * data_dir,
const char * key,
char * file_name,
Expand Down
46 changes: 0 additions & 46 deletions src/spx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,6 @@
#include <string.h>
#include "spx_utils.h"

char * spx_utils_resolve_confined_file_absolute_path(
const char * root_dir,
const char * relative_path,
const char * suffix,
char * dst,
size_t size
) {
if (size < PATH_MAX) {
spx_utils_die("size < PATH_MAX");
}

char absolute_file_path[PATH_MAX];

snprintf(
absolute_file_path,
sizeof(absolute_file_path),
"%s%s%s",
root_dir,
relative_path,
suffix == NULL ? "" : suffix
);

if (realpath(absolute_file_path, dst) == NULL) {
return NULL;
}

char root_dir_real_path[PATH_MAX];
if (realpath(root_dir, root_dir_real_path) == NULL) {
return NULL;
}

char expected_path_prefix[PATH_MAX + 1];
snprintf(
expected_path_prefix,
sizeof(expected_path_prefix),
"%s/",
root_dir_real_path
);

if (! spx_utils_str_starts_with(dst, expected_path_prefix)) {
return NULL;
}

return dst;
}

char * spx_utils_json_escape(char * dst, const char * src, size_t limit)
{
size_t i = 0;
Expand Down
9 changes: 0 additions & 9 deletions src/spx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define SPX_UTILS_H_DEFINED

#include <stddef.h>
#include <limits.h> /* PATH_MAX */

#define SPX_UTILS_TOKENIZE_STRING(str, delim, token, size, block) \
do { \
Expand Down Expand Up @@ -50,14 +49,6 @@ do { \
} \
} while (0)

char * spx_utils_resolve_confined_file_absolute_path(
const char * root_dir,
const char * relative_path,
const char * suffix,
char * dst,
size_t size
);

char * spx_utils_json_escape(char * dst, const char * src, size_t limit);
int spx_utils_str_starts_with(const char * str, const char * prefix);
int spx_utils_str_ends_with(const char * str, const char * suffix);
Expand Down

0 comments on commit 8c52c5e

Please sign in to comment.