Changeset 18 in Main for trunk/Server/server.c
- Timestamp:
- Sep 14, 2024, 9:42:40 AM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Server/server.c
r17 r18 8 8 #include "tw_config.h" 9 9 #include "tw_http.h" 10 #include "tw_module.h" 11 #include "tw_version.h" 10 12 11 13 #include <unistd.h> … … 13 15 #include <stdbool.h> 14 16 17 #include <cm_string.h> 15 18 #include <cm_log.h> 16 19 … … 28 31 29 32 extern struct tw_config config; 33 extern char tw_server[]; 30 34 31 35 fd_set fdset; … … 130 134 return SSL_write(ssl, data, len); 131 135 } 136 } 137 138 #define ERROR_400 \ 139 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 140 "<html>\n" \ 141 " <head>\n" \ 142 " <title>400 Bad Request</title>" \ 143 " </head>\n" \ 144 " <body>\n" \ 145 " <h1>Bad Request</h1>\n" \ 146 " <hr>\n" \ 147 " ", \ 148 address, \ 149 "\n" \ 150 " </body>\n" \ 151 "</html>\n" 152 153 #define ERROR_401 \ 154 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 155 "<html>\n" \ 156 " <head>\n" \ 157 " <title>401 Unauthorized</title>" \ 158 " </head>\n" \ 159 " <body>\n" \ 160 " <h1>Unauthorized</h1>\n" \ 161 " <hr>\n" \ 162 " ", \ 163 address, \ 164 "\n" \ 165 " </body>\n" \ 166 "</html>\n" 167 168 #define ERROR_403 \ 169 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 170 "<html>\n" \ 171 " <head>\n" \ 172 " <title>403 Forbidden</title>" \ 173 " </head>\n" \ 174 " <body>\n" \ 175 " <h1>Forbidden</h1>\n" \ 176 " <hr>\n" \ 177 " ", \ 178 address, \ 179 "\n" \ 180 " </body>\n" \ 181 "</html>\n" 182 183 #define ERROR_404 \ 184 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \ 185 "<html>\n" \ 186 " <head>\n" \ 187 " <title>404 Not Found</title>" \ 188 " </head>\n" \ 189 " <body>\n" \ 190 " <h1>Not Found</h1>\n" \ 191 " <hr>\n" \ 192 " ", \ 193 address, \ 194 "\n" \ 195 " </body>\n" \ 196 "</html>\n" 197 198 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, const unsigned char* doc, size_t size) { 199 char construct[512]; 200 sprintf(construct, "%llu", (unsigned long long)size); 201 tw_write(ssl, sock, "HTTP/1.1 ", 9); 202 tw_write(ssl, sock, (char*)status, strlen(status)); 203 tw_write(ssl, sock, "\r\n", 2); 204 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2); 205 tw_write(ssl, sock, (char*)type, strlen(type)); 206 tw_write(ssl, sock, "\r\n", 2); 207 tw_write(ssl, sock, "Server: ", 6 + 2); 208 tw_write(ssl, sock, tw_server, strlen(tw_server)); 209 tw_write(ssl, sock, "\r\n", 2); 210 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2); 211 tw_write(ssl, sock, construct, strlen(construct)); 212 tw_write(ssl, sock, "\r\n", 2); 213 tw_write(ssl, sock, "\r\n", 2); 214 size_t incr = 0; 215 while(1) { 216 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128); 217 incr += 128; 218 size -= 128; 219 if(size <= 0) break; 220 } 221 } 222 223 const char* tw_http_status(int code) { 224 if(code == 400) { 225 return "400 Bad Request"; 226 } else { 227 return "400 Bad Request"; 228 } 229 } 230 231 char* tw_http_default_error(int code, char* name, int port) { 232 char address[1024]; 233 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port); 234 if(code == 400) { 235 return cm_strcat3(ERROR_400); 236 } else { 237 return cm_strcat3(ERROR_400); 238 } 239 } 240 241 void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) { 242 char* str = tw_http_default_error(error, name, port); 243 tw_process_page(ssl, sock, tw_http_status(error), "text/html", str, strlen(str)); 244 free(str); 132 245 } 133 246 … … 162 275 int ret = tw_http_parse(s, sock, &req); 163 276 if(ret == 0) { 277 } else { 278 tw_http_error(s, sock, 400, name, port); 164 279 } 165 280 cleanup:
Note:
See TracChangeset
for help on using the changeset viewer.