From f4d4898c075b1f65e8f91dcb827d06f385262e2d Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Fri, 24 Nov 2006 15:46:43 +0000 Subject: [PATCH] Factorize HTTPd error messages --- src/network/httpd.c | 129 +++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 86 deletions(-) diff --git a/src/network/httpd.c b/src/network/httpd.c index 7dc04057b3..3d0d608ade 100644 --- a/src/network/httpd.c +++ b/src/network/httpd.c @@ -267,7 +267,7 @@ static const char *httpd_MimeFromUrl( const char *psz_url ) return "application/octet-stream"; } -#if 0 + typedef struct { int i_code; @@ -347,7 +347,38 @@ static const char *httpd_ReasonFromCode( int i_code ) assert( ( i_code >= 100 ) && ( i_code <= 599 ) ); return psz_fallback_reason[(i_code / 100) - 1]; } -#endif + + +static size_t httpd_HtmlError (char **body, int code, const char *url) +{ + const char *errname = httpd_ReasonFromCode (code); + assert (errname != NULL); + + int res = asprintf (body, + "\n" + "\n" + "\n" + "\n" + "%s\n" + "\n" + "\n" + "

%d %s%s%s%s

\n" + "
\n" + "VideoLAN\n" + "\n" + "\n", errname, code, errname, + (url ? " (" : ""), (url ?: ""), (url ? ")" : "")); + + if (res == -1) + { + *body = NULL; + return 0; + } + + return (size_t)res; +} + /***************************************************************************** * High Level Functions: httpd_file_t @@ -637,22 +668,7 @@ static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys, answer->i_status = 301; answer->psz_status = strdup( "Moved Permanently" ); - answer->i_body = asprintf( &p_body, - "\n" - "\n" - "\n" - "\n" - "Redirection\n" - "\n" - "\n" - "

You should be " - "redirected

\n" - "
\n" - "

VideoLAN\n

" - "
\n" - "\n" - "\n", rdir->psz_dst ); + answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst); answer->p_body = (unsigned char *)p_body; /* XXX check if it's ok or we need to set an absolute url */ @@ -2097,7 +2113,7 @@ static void httpd_HostThread( httpd_host_t *host ) } else { - uint8_t *p; + char *p; /* unimplemented */ answer->i_proto = query->i_proto ; @@ -2106,24 +2122,8 @@ static void httpd_HostThread( httpd_host_t *host ) answer->i_status = 501; answer->psz_status = strdup( "Unimplemented" ); - p = answer->p_body = malloc( 1000 ); - - p += sprintf( (char *)p, - "" - "\n" - "\n" - "\n" - "Error 501\n" - "\n" - "\n" - "

501 Unimplemented

\n" - "
\n" - "VideoLAN\n" - "\n" - "\n" ); - - answer->i_body = p - answer->p_body; + answer->i_body = httpd_HtmlError (&p, 501, NULL); + answer->p_body = (uint8_t *)p; httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body ); cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */ @@ -2222,77 +2222,34 @@ static void httpd_HostThread( httpd_host_t *host ) if( answer ) { - uint8_t *p; + char *p; answer->i_proto = query->i_proto; answer->i_type = HTTPD_MSG_ANSWER; answer->i_version= 0; - p = answer->p_body = malloc( 1000 + strlen(query->psz_url) ); if( b_hosts_failed ) { answer->i_status = 403; answer->psz_status = strdup( "Forbidden" ); - - /* FIXME: lots of code duplication */ - p += sprintf( (char *)p, - "" - "\n" - "\n" - "\n" - "Error 403\n" - "\n" - "\n" - "

403 Forbidden (%s)

\n" - "
\n" - "

VideoLAN

\n" - "\n" - "\n", query->psz_url ); } else if( b_auth_failed ) { answer->i_status = 401; answer->psz_status = strdup( "Authorization Required" ); - - p += sprintf( (char *)p, - "" - "\n" - "\n" - "\n" - "Error 401\n" - "\n" - "\n" - "

401 Authorization Required (%s)

\n" - "
\n" - "

VideoLAN

\n" - "\n" - "\n", query->psz_url ); } else { /* no url registered */ answer->i_status = 404; answer->psz_status = strdup( "Not found" ); - - p += sprintf( (char *)p, - "" - "\n" - "\n" - "\n" - "Error 404\n" - "\n" - "\n" - "

404 Resource not found(%s)

\n" - "
\n" - "

VideoLAN

\n" - "\n" - "\n", query->psz_url ); } - answer->i_body = p - answer->p_body; + answer->i_body = httpd_HtmlError (&p, + answer->i_status, + query->psz_url); + answer->p_body = (uint8_t *)p; + cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */ httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body ); httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" ); -- 2.39.5