Skip to content

Commit e233e9f

Browse files
committed
Consolidate duplicated code for setting up output templates
1 parent 5ab1575 commit e233e9f

File tree

1 file changed

+41
-99
lines changed

1 file changed

+41
-99
lines changed

ngx_http_uploadprogress_module.c

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *
9999
static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
100100
static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
101101
static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
102-
static char* ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
103-
static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
104-
static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
105-
static char* ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
106-
static char* ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
102+
static char *ngx_http_upload_progress_output_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
103+
static char *ngx_http_upload_progress_java_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
104+
static char *ngx_http_upload_progress_json_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
105+
static char *ngx_http_upload_progress_jsonp_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
106+
static char *ngx_http_upload_progress_json_multiple_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
107+
static char *ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
107108
static void ngx_clean_old_connections(ngx_event_t * ev);
108109
static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
109110

@@ -1831,133 +1832,74 @@ ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *co
18311832
}
18321833

18331834
static char*
1834-
ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1835-
{
1835+
ngx_http_upload_progress_output_internal(
1836+
ngx_conf_t *cf, ngx_command_t *cmd, void *conf, ngx_uint_t multi,
1837+
ngx_str_t content_type, ngx_str_t *templates
1838+
) {
18361839
ngx_http_uploadprogress_conf_t *upcf = conf;
18371840
ngx_http_uploadprogress_template_t *t;
18381841
ngx_uint_t i;
18391842
char* rc;
18401843

1841-
upcf->json_multiple = 0;
1844+
upcf->json_multiple = multi;
18421845

18431846
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
18441847

18451848
for (i = 0; i < upcf->templates.nelts; i++) {
1846-
rc = ngx_http_upload_progress_set_template(
1847-
cf, t + i, ngx_http_uploadprogress_java_defaults + i);
1848-
1849+
rc = ngx_http_upload_progress_set_template(cf, t + i, templates + i);
18491850
if (rc != NGX_CONF_OK) {
18501851
return rc;
18511852
}
18521853
}
18531854

1854-
upcf->content_type = (ngx_str_t)ngx_string("text/javascript");
1855+
upcf->content_type = content_type;
18551856

18561857
return NGX_CONF_OK;
18571858
}
18581859

18591860
static char*
1860-
ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1861+
ngx_http_upload_progress_java_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
18611862
{
1862-
ngx_http_uploadprogress_conf_t *upcf = conf;
1863-
ngx_http_uploadprogress_template_t *t;
1864-
ngx_uint_t i;
1865-
char* rc;
1866-
1867-
upcf->json_multiple = 0;
1868-
1869-
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1870-
1871-
for (i = 0; i < upcf->templates.nelts; i++) {
1872-
rc = ngx_http_upload_progress_set_template(
1873-
cf, t + i, ngx_http_uploadprogress_json_defaults + i);
1874-
1875-
if (rc != NGX_CONF_OK) {
1876-
return rc;
1877-
}
1878-
}
1879-
1880-
upcf->content_type = (ngx_str_t)ngx_string("application/json");
1881-
1882-
return NGX_CONF_OK;
1863+
return ngx_http_upload_progress_output_internal(
1864+
cf, cmd, conf, 0,
1865+
(ngx_str_t)ngx_string("text/javascript"),
1866+
ngx_http_uploadprogress_java_defaults);
18831867
}
18841868

18851869
static char*
1886-
ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1870+
ngx_http_upload_progress_json_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
18871871
{
1888-
ngx_http_uploadprogress_conf_t *upcf = conf;
1889-
ngx_http_uploadprogress_template_t *t;
1890-
ngx_uint_t i;
1891-
char* rc;
1892-
1893-
upcf->json_multiple = 0;
1894-
1895-
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1896-
1897-
for (i = 0; i < upcf->templates.nelts; i++) {
1898-
rc = ngx_http_upload_progress_set_template(
1899-
cf, t + i, ngx_http_uploadprogress_jsonp_defaults + i);
1900-
1901-
if (rc != NGX_CONF_OK) {
1902-
return rc;
1903-
}
1904-
}
1905-
1906-
upcf->content_type = (ngx_str_t)ngx_string("application/javascript");
1907-
1908-
return NGX_CONF_OK;
1872+
return ngx_http_upload_progress_output_internal(
1873+
cf, cmd, conf, 0,
1874+
(ngx_str_t)ngx_string("application/json"),
1875+
ngx_http_uploadprogress_json_defaults);
19091876
}
19101877

19111878
static char*
1912-
ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1879+
ngx_http_upload_progress_jsonp_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
19131880
{
1914-
ngx_http_uploadprogress_conf_t *upcf = conf;
1915-
ngx_http_uploadprogress_template_t *t;
1916-
ngx_uint_t i;
1917-
char* rc;
1918-
1919-
upcf->json_multiple = 1;
1920-
1921-
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1922-
1923-
for (i = 0; i < upcf->templates.nelts; i++) {
1924-
rc = ngx_http_upload_progress_set_template(
1925-
cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
1926-
1927-
if (rc != NGX_CONF_OK) {
1928-
return rc;
1929-
}
1930-
}
1931-
1932-
upcf->content_type = (ngx_str_t)ngx_string("application/json");
1933-
1934-
return NGX_CONF_OK;
1881+
return ngx_http_upload_progress_output_internal(
1882+
cf, cmd, conf, 0,
1883+
(ngx_str_t)ngx_string("application/javascript"),
1884+
ngx_http_uploadprogress_jsonp_defaults);
19351885
}
19361886

19371887
static char*
1938-
ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
1888+
ngx_http_upload_progress_json_multiple_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
19391889
{
1940-
ngx_http_uploadprogress_conf_t *upcf = conf;
1941-
ngx_http_uploadprogress_template_t *t;
1942-
ngx_uint_t i;
1943-
char* rc;
1944-
1945-
upcf->json_multiple = 1;
1946-
1947-
t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
1948-
1949-
for (i = 0; i < upcf->templates.nelts; i++) {
1950-
rc = ngx_http_upload_progress_set_template(
1951-
cf, t + i, ngx_http_uploadprogress_jsonp_multiple_defaults + i);
1952-
1953-
if (rc != NGX_CONF_OK) {
1954-
return rc;
1955-
}
1956-
}
1957-
1958-
upcf->content_type = (ngx_str_t)ngx_string("application/json");
1890+
return ngx_http_upload_progress_output_internal(
1891+
cf, cmd, conf, 1,
1892+
(ngx_str_t)ngx_string("application/json"),
1893+
ngx_http_uploadprogress_json_multiple_defaults);
1894+
}
19591895

1960-
return NGX_CONF_OK;
1896+
static char*
1897+
ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1898+
{
1899+
return ngx_http_upload_progress_output_internal(
1900+
cf, cmd, conf, 1,
1901+
(ngx_str_t)ngx_string("application/json"),
1902+
ngx_http_uploadprogress_jsonp_multiple_defaults);
19611903
}
19621904

19631905
static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,

0 commit comments

Comments
 (0)