@@ -11,35 +11,36 @@ enum { AUTH_OK, AUTH_FAIL, AUTH_ERROR };
1111static char * html_cache = NULL ;
1212static size_t html_cache_len = 0 ;
1313
14- static int check_auth (struct lws * wsi , struct pss_http * pss ) {
15- if (server -> credential == NULL ) return AUTH_OK ;
16-
17- char buf [256 ];
18- int len = lws_hdr_copy (wsi , buf , sizeof (buf ), WSI_TOKEN_HTTP_AUTHORIZATION );
19- if (len >= 7 && strstr (buf , "Basic " )) {
20- if (!strcmp (buf + 6 , server -> credential )) return AUTH_OK ;
21- }
22-
14+ static int send_unauthorized (struct lws * wsi , unsigned int code , enum lws_token_indexes header ) {
2315 unsigned char buffer [1024 + LWS_PRE ], * p , * end ;
2416 p = buffer + LWS_PRE ;
2517 end = p + sizeof (buffer ) - LWS_PRE ;
2618
27- char * body = strdup ("401 Unauthorized\n" );
28- size_t n = strlen (body );
29-
30- if (lws_add_http_header_status (wsi , HTTP_STATUS_UNAUTHORIZED , & p , end ) ||
31- lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_WWW_AUTHENTICATE ,
32- (unsigned char * )"Basic realm=\"ttyd\"" , 18 , & p , end ) ||
33- lws_add_http_header_content_length (wsi , n , & p , end ) ||
34- lws_finalize_http_header (wsi , & p , end ) ||
19+ if (lws_add_http_header_status (wsi , code , & p , end ) ||
20+ lws_add_http_header_by_token (wsi , header , (unsigned char * )"Basic realm=\"ttyd\"" , 18 , & p , end ) ||
21+ lws_add_http_header_content_length (wsi , 0 , & p , end ) || lws_finalize_http_header (wsi , & p , end ) ||
3522 lws_write (wsi , buffer + LWS_PRE , p - (buffer + LWS_PRE ), LWS_WRITE_HTTP_HEADERS ) < 0 )
36- return AUTH_ERROR ;
23+ return AUTH_FAIL ;
3724
38- pss -> buffer = pss -> ptr = body ;
39- pss -> len = n ;
40- lws_callback_on_writable (wsi );
25+ return lws_http_transaction_completed (wsi ) ? AUTH_FAIL : AUTH_ERROR ;
26+ }
4127
42- return AUTH_FAIL ;
28+ static int check_auth (struct lws * wsi , struct pss_http * pss ) {
29+ if (server -> auth_header != NULL ) {
30+ if (lws_hdr_custom_length (wsi , server -> auth_header , strlen (server -> auth_header )) > 0 ) return AUTH_OK ;
31+ return send_unauthorized (wsi , HTTP_STATUS_PROXY_AUTH_REQUIRED , WSI_TOKEN_HTTP_PROXY_AUTHENTICATE );
32+ }
33+
34+ if (server -> credential != NULL ) {
35+ char buf [256 ];
36+ int len = lws_hdr_copy (wsi , buf , sizeof (buf ), WSI_TOKEN_HTTP_AUTHORIZATION );
37+ if (len >= 7 && strstr (buf , "Basic " )) {
38+ if (!strcmp (buf + 6 , server -> credential )) return AUTH_OK ;
39+ }
40+ return send_unauthorized (wsi , HTTP_STATUS_UNAUTHORIZED , WSI_TOKEN_HTTP_WWW_AUTHENTICATE );
41+ }
42+
43+ return AUTH_OK ;
4344}
4445
4546static bool accept_gzip (struct lws * wsi ) {
@@ -89,8 +90,7 @@ static void access_log(struct lws *wsi, const char *path) {
8990 lwsl_notice ("HTTP %s - %s\n" , path , rip );
9091}
9192
92- int callback_http (struct lws * wsi , enum lws_callback_reasons reason , void * user , void * in ,
93- size_t len ) {
93+ int callback_http (struct lws * wsi , enum lws_callback_reasons reason , void * user , void * in , size_t len ) {
9494 struct pss_http * pss = (struct pss_http * )user ;
9595 unsigned char buffer [4096 + LWS_PRE ], * p , * end ;
9696 char buf [256 ];
@@ -118,8 +118,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
118118 size_t n = sprintf (buf , "{\"token\": \"%s\"}" , credential );
119119 if (lws_add_http_header_status (wsi , HTTP_STATUS_OK , & p , end ) ||
120120 lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_CONTENT_TYPE ,
121- (unsigned char * )"application/json;charset=utf-8" , 30 , & p ,
122- end ) ||
121+ (unsigned char * )"application/json;charset=utf-8" , 30 , & p , end ) ||
123122 lws_add_http_header_content_length (wsi , (unsigned long )n , & p , end ) ||
124123 lws_finalize_http_header (wsi , & p , end ) ||
125124 lws_write (wsi , buffer + LWS_PRE , p - (buffer + LWS_PRE ), LWS_WRITE_HTTP_HEADERS ) < 0 )
@@ -134,11 +133,9 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
134133 // redirects `/base-path` to `/base-path/`
135134 if (strcmp (pss -> path , endpoints .parent ) == 0 ) {
136135 if (lws_add_http_header_status (wsi , HTTP_STATUS_FOUND , & p , end ) ||
137- lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_LOCATION ,
138- (unsigned char * )endpoints .index ,
136+ lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_LOCATION , (unsigned char * )endpoints .index ,
139137 (int )strlen (endpoints .index ), & p , end ) ||
140- lws_add_http_header_content_length (wsi , 0 , & p , end ) ||
141- lws_finalize_http_header (wsi , & p , end ) ||
138+ lws_add_http_header_content_length (wsi , 0 , & p , end ) || lws_finalize_http_header (wsi , & p , end ) ||
142139 lws_write (wsi , buffer + LWS_PRE , p - (buffer + LWS_PRE ), LWS_WRITE_HTTP_HEADERS ) < 0 )
143140 return 1 ;
144141 goto try_to_reuse ;
@@ -157,15 +154,14 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
157154 char * output = (char * )index_html ;
158155 size_t output_len = index_html_len ;
159156 if (lws_add_http_header_status (wsi , HTTP_STATUS_OK , & p , end ) ||
160- lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_CONTENT_TYPE ,
161- ( const unsigned char * ) content_type , 9 , & p , end ))
157+ lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_CONTENT_TYPE , ( const unsigned char * ) content_type , 9 , & p ,
158+ end ))
162159 return 1 ;
163160#ifdef LWS_WITH_HTTP_STREAM_COMPRESSION
164161 if (!uncompress_html (& output , & output_len )) return 1 ;
165162#else
166163 if (accept_gzip (wsi )) {
167- if (lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_CONTENT_ENCODING ,
168- (unsigned char * )"gzip" , 4 , & p , end ))
164+ if (lws_add_http_header_by_token (wsi , WSI_TOKEN_HTTP_CONTENT_ENCODING , (unsigned char * )"gzip" , 4 , & p , end ))
169165 return 1 ;
170166 } else {
171167 if (!uncompress_html (& output , & output_len )) return 1 ;
0 commit comments