Changeset 32 in Main for trunk/Server/server.c
- Timestamp:
- Sep 16, 2024, 9:42:19 PM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Server/server.c
r31 r32 16 16 #include <stdarg.h> 17 17 #include <sys/stat.h> 18 #include <time.h> 18 19 19 20 #include <cm_string.h> … … 24 25 #include <winsock2.h> 25 26 #include <process.h> 27 28 #include "strptime.h" 26 29 #else 27 30 #include <sys/select.h> … … 170 173 "</html>\n" 171 174 172 void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers ) {175 void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) { 173 176 char construct[512]; 177 if(mtime != 0 && cmtime != 0 && mtime <= cmtime) { 178 status = "304 Not Modified"; 179 type = NULL; 180 size = 0; 181 headers = NULL; 182 f = NULL; 183 doc = NULL; 184 } 174 185 sprintf(construct, "%llu", (unsigned long long)size); 175 186 tw_write(ssl, sock, "HTTP/1.1 ", 9); … … 188 199 tw_write(ssl, sock, construct, strlen(construct)); 189 200 tw_write(ssl, sock, "\r\n", 2); 201 if(mtime != 0) { 202 struct tm* tm = gmtime(&mtime); 203 char date[513]; 204 strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm); 205 tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2); 206 tw_write(ssl, sock, date, strlen(date)); 207 tw_write(ssl, sock, "\r\n", 2); 208 } 190 209 } 191 210 int i; … … 215 234 } 216 235 217 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size ) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL); }236 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, time_t mtime, time_t cmtime) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL, mtime, cmtime); } 218 237 219 238 const char* tw_http_status(int code) { … … 258 277 void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) { 259 278 char* str = tw_http_default_error(error, name, port); 260 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str) );279 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0); 261 280 free(str); 262 281 } … … 389 408 char* vhost = cm_strdup(config.hostname); 390 409 int i; 410 time_t cmtime = 0; 391 411 for(i = 0; req.headers[i] != NULL; i += 2) { 392 412 if(cm_strcaseequ(req.headers[i], "Host")) { 393 413 free(vhost); 394 vhost = req.headers[i + 1]; 395 break; 414 vhost = cm_strdup(req.headers[i + 1]); 415 } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) { 416 struct tm tm; 417 strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm); 418 #ifdef __MINGW32__ 419 cmtime = _mkgmtime(&tm); 420 #else 421 cmtime = timegm(&tm); 422 #endif 396 423 } 397 424 } … … 435 462 if(req.path[strlen(req.path) - 1] != '/') { 436 463 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL}; 437 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers );464 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers, 0, 0); 438 465 free(headers[1]); 439 466 } else { … … 458 485 stat(p, &st); 459 486 char* mime = tw_get_mime(ext, vhost_entry); 460 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size );487 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0); 461 488 fclose(f); 462 489 free(p); … … 572 599 addstring(&str, " </body>\n"); 573 600 addstring(&str, "</html>\n"); 574 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str) );601 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0); 575 602 free(str); 576 603 } … … 589 616 if(ext != NULL) free(ext); 590 617 FILE* f = fopen(path, "rb"); 591 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size );618 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime); 592 619 fclose(f); 593 620 } … … 599 626 free(vhost); 600 627 free(host); 628 tw_free_request(&req); 601 629 } else if(ret == -1) { 602 630 } else {
Note:
See TracChangeset
for help on using the changeset viewer.