1919#include "autoindex.h"
2020#include "log/log.h"
2121
22- bool
23- fh_autoindex_handle (struct fh_autoindex * autoindex )
22+ static struct fh_link end_chunk_link = {
23+ .is_eos = true,
24+ .is_start = false,
25+ .next = NULL ,
26+ .buf = & (struct fh_buf ) {
27+ .type = FH_BUF_DATA ,
28+ .freeable = false,
29+ .attrs .mem .cap = 5 ,
30+ .attrs .mem .len = 5 ,
31+ .attrs .mem .rd_only = true,
32+ .attrs .mem .data = (uint8_t * ) "0\r\n\r\n" ,
33+ },
34+ };
35+
36+ static bool
37+ fh_autoindex_handle_chunked (struct fh_autoindex * autoindex )
2438{
2539 const struct fh_request * request = autoindex -> request ;
2640 struct fh_response * response = autoindex -> response ;
@@ -43,8 +57,8 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
4357 pool_t * pool = autoindex -> response -> pool ;
4458 uint16_t port = autoindex -> conn -> extra -> port ;
4559 size_t index_start_chunk_len
46- = resource_index_start_html_len - 23 + (3 * request -> uri_len );
47- size_t index_end_chunk_len = resource_index_end_html_len - 17
60+ = resource_index_start_html_len - 12 + (3 * request -> uri_len );
61+ size_t index_end_chunk_len = resource_index_end_html_len - 6
4862 + autoindex -> conn -> extra -> host_len
4963 + (port < 10 ? 1
5064 : port < 100 ? 2
@@ -56,12 +70,10 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
5670 size_t index_end_len = index_end_chunk_len + 32 + 4 ;
5771
5872 struct fh_link * end_link ;
59- struct fh_link * end_chunk_link ;
60- struct fh_link * start_link = fh_pool_alloc (
61- pool ,
62- (sizeof (* start_link ) + sizeof (* start_link -> buf ) + index_start_len + 1
63- + sizeof (* end_link ) + sizeof (* end_link -> buf ) + index_end_len + 1
64- + sizeof (* end_chunk_link ) + sizeof (* end_chunk_link -> buf )));
73+ struct fh_link * start_link
74+ = fh_pool_alloc (pool , (sizeof (* start_link ) + sizeof (* start_link -> buf )
75+ + index_start_len + 1 + sizeof (* end_link )
76+ + sizeof (* end_link -> buf ) + index_end_len + 1 ));
6577
6678 if (!start_link )
6779 {
@@ -83,44 +95,57 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
8395 end_link -> next = NULL ;
8496 end_link -> is_eos = false;
8597
86- end_chunk_link = (struct fh_link * ) (end_link -> buf -> attrs .mem .data
87- + index_end_len + 1 );
88- end_chunk_link -> buf = (struct fh_buf * ) (end_chunk_link + 1 );
89- end_chunk_link -> buf -> attrs .mem .data = (uint8_t * ) "0\r\n\r\n" ;
90- end_chunk_link -> buf -> type = FH_BUF_DATA ;
91- end_chunk_link -> buf -> attrs .mem .cap = end_chunk_link -> buf -> attrs .mem .len = 5 ;
92- end_chunk_link -> next = NULL ;
93- end_chunk_link -> is_eos = true;
94- end_chunk_link -> buf -> attrs .mem .rd_only = true;
95-
96- int rc ;
97-
98- if ((rc = snprintf (
99- (char * ) start_link -> buf -> attrs .mem .data , index_start_len + 1 ,
100- resource_index_start_html , index_start_chunk_len , '\r' , '\n' ,
101- (int ) request -> uri_len , request -> uri , (int ) request -> uri_len ,
102- request -> uri , (int ) request -> uri_len , request -> uri , '\r' , '\n' ))
98+ end_chunk_link .buf -> attrs .mem .data = (uint8_t * ) "0\r\n\r\n" ;
99+ end_chunk_link .buf -> attrs .mem .cap = end_chunk_link .buf -> attrs .mem .len = 5 ;
100+
101+ int rc1 , rc2 ;
102+
103+ if ((rc1 = snprintf ((char * ) start_link -> buf -> attrs .mem .data , 34 ,
104+ "%zx\r\n" , index_start_chunk_len ))
105+ < 0 )
106+ {
107+ response -> status = FH_STATUS_INTERNAL_SERVER_ERROR ;
108+ return true;
109+ }
110+
111+ if ((rc2 = snprintf ((char * ) start_link -> buf -> attrs .mem .data + rc1 ,
112+ index_start_len + 1 - rc1 , resource_index_start_html ,
113+ (int ) request -> uri_len , request -> uri ,
114+ (int ) request -> uri_len , request -> uri ,
115+ (int ) request -> uri_len , request -> uri ))
103116 < 0 )
104117 {
105118 response -> status = FH_STATUS_INTERNAL_SERVER_ERROR ;
106119 return true;
107120 }
108121
122+ start_link -> buf -> attrs .mem .data [rc1 + rc2 ] = '\r' ;
123+ start_link -> buf -> attrs .mem .data [rc1 + rc2 + 1 ] = '\n' ;
109124 start_link -> buf -> attrs .mem .cap = start_link -> buf -> attrs .mem .len
110- = (size_t ) rc ;
125+ = (size_t ) (rc1 + rc2 + 2 );
126+
127+ if ((rc1 = snprintf ((char * ) end_link -> buf -> attrs .mem .data , 34 , "%zx\r\n" ,
128+ index_end_chunk_len ))
129+ < 0 )
130+ {
131+ response -> status = FH_STATUS_INTERNAL_SERVER_ERROR ;
132+ return true;
133+ }
111134
112- if ((rc
113- = snprintf ((char * ) end_link -> buf -> attrs .mem .data , index_end_len + 1 ,
114- resource_index_end_html , index_end_chunk_len , '\r' , '\n' ,
115- (int ) autoindex -> conn -> extra -> host_len ,
116- autoindex -> conn -> extra -> host , port , '\r' , '\n' ))
135+ if ((rc2 = snprintf ((char * ) end_link -> buf -> attrs .mem .data + rc1 ,
136+ index_end_len + 1 - rc1 , resource_index_end_html ,
137+ (int ) autoindex -> conn -> extra -> host_len ,
138+ autoindex -> conn -> extra -> host , port ))
117139 < 0 )
118140 {
119141 response -> status = FH_STATUS_INTERNAL_SERVER_ERROR ;
120142 return true;
121143 }
122144
123- end_link -> buf -> attrs .mem .cap = end_link -> buf -> attrs .mem .len = (size_t ) rc ;
145+ end_link -> buf -> attrs .mem .data [rc1 + rc2 ] = '\r' ;
146+ end_link -> buf -> attrs .mem .data [rc1 + rc2 + 1 ] = '\n' ;
147+ end_link -> buf -> attrs .mem .cap = end_link -> buf -> attrs .mem .len
148+ = (size_t ) (rc1 + rc2 + 2 );
124149
125150 struct dirent64 * * namelist ;
126151 int namelist_count = 0 ;
@@ -309,8 +334,8 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
309334 tail -> next = end_link ;
310335 tail = end_link ;
311336
312- tail -> next = end_chunk_link ;
313- tail = end_chunk_link ;
337+ tail -> next = & end_chunk_link ;
338+ tail = & end_chunk_link ;
314339
315340 response -> encoding = FH_ENCODING_CHUNKED ;
316341 response -> body_start = start_link ;
@@ -320,3 +345,25 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
320345 fh_pr_debug ("Response generated successfully" );
321346 return true;
322347}
348+
349+ static bool
350+ fh_autoindex_handle_plain (struct fh_autoindex * autoindex )
351+ {
352+ const struct fh_request * request = autoindex -> request ;
353+ struct fh_response * response = autoindex -> response ;
354+
355+ return true;
356+ }
357+
358+ bool
359+ fh_autoindex_handle (struct fh_autoindex * autoindex )
360+ {
361+ switch (autoindex -> request -> protocol )
362+ {
363+ case FH_PROTOCOL_HTTP_1_0 :
364+ return fh_autoindex_handle_plain (autoindex );
365+
366+ default :
367+ return fh_autoindex_handle_chunked (autoindex );
368+ }
369+ }
0 commit comments