/[jscoverage]/trunk/jscoverage-server.c
ViewVC logotype

Diff of /trunk/jscoverage-server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 447 by siliconforks, Sun Aug 9 16:21:27 2009 UTC revision 583 by siliconforks, Sat Sep 11 20:20:14 2010 UTC
# Line 1  Line 1 
1  /*  /*
2      jscoverage-server.c - JSCoverage server main routine      jscoverage-server.c - JSCoverage server main routine
3      Copyright (C) 2008, 2009 siliconforks.com      Copyright (C) 2008, 2009, 2010 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by      it under the terms of the GNU General Public License as published by
# Line 42  Line 42 
42  const char * jscoverage_encoding = "ISO-8859-1";  const char * jscoverage_encoding = "ISO-8859-1";
43  bool jscoverage_highlight = true;  bool jscoverage_highlight = true;
44    
45    struct in_addr jscoverage_ip_address;
46    
47  typedef struct SourceCache {  typedef struct SourceCache {
48    char * url;    char * url;
49    uint16_t * characters;    uint16_t * characters;
# Line 398  Line 400 
400    }    }
401  }  }
402    
403  static bool should_instrument_request(HTTPExchange * exchange) {  static bool should_instrument_request(HTTPExchange * exchange, const char * uri) {
404    if (! is_javascript(exchange)) {    if (! is_javascript(exchange)) {
405      return false;      return false;
406    }    }
407    
408    if (is_no_instrument(HTTPExchange_get_request_uri(exchange))) {    if (is_no_instrument(uri)) {
409      return false;      return false;
410    }    }
411    
# Line 696  Line 698 
698        return;        return;
699      }      }
700    
701      /* allow only from localhost */      /* check client IP address */
702      struct sockaddr_in client;      struct sockaddr_in client;
703      if (HTTPExchange_get_peer(exchange, &client) != 0) {      if (HTTPExchange_get_peer(exchange, &client) != 0) {
704        send_response(exchange, 500, "Cannot get client address\n");        send_response(exchange, 500, "Cannot get client address\n");
705        return;        return;
706      }      }
707      if (client.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {  
708        /*
709        We allow connections from loopback 127.0.0.0 to 127.255.255.255 (127/8)
710        and from the address specified with --ip-address
711        */
712        bool client_allowed = false;
713    
714        if (client.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
715          client_allowed = true;
716        }
717    
718        if ((ntohl(client.sin_addr.s_addr) & 0xFF000000) == 0x7F000000) {
719          client_allowed = true;
720        }
721    
722        if (client.sin_addr.s_addr == jscoverage_ip_address.s_addr) {
723          client_allowed = true;
724        }
725    
726        if (! client_allowed) {
727        send_response(exchange, 403, "This operation can be performed only by localhost\n");        send_response(exchange, 403, "This operation can be performed only by localhost\n");
728        return;        return;
729      }      }
# Line 801  Line 822 
822    
823    /* create a new exchange */    /* create a new exchange */
824    server_exchange = HTTPExchange_new(server_connection);    server_exchange = HTTPExchange_new(server_connection);
825    
826    HTTPExchange_set_method(server_exchange, HTTPExchange_get_method(client_exchange));    HTTPExchange_set_method(server_exchange, HTTPExchange_get_method(client_exchange));
827    HTTPExchange_set_request_uri(server_exchange, HTTPExchange_get_request_uri(client_exchange));  
828      /* don't send full URI to origin server - just send abs_path and query */
829      const char * query = HTTPExchange_get_query(client_exchange);
830      char * origin_server_request_uri;
831      if (query == NULL) {
832        origin_server_request_uri = xstrdup(abs_path);
833      }
834      else {
835        size_t abs_path_length = strlen(abs_path);
836        size_t query_length = strlen(query);
837        size_t origin_server_request_uri_length = addst(abs_path_length, query_length);
838        origin_server_request_uri_length = addst(origin_server_request_uri_length, 2);
839        origin_server_request_uri = xmalloc(origin_server_request_uri_length);
840        strcpy(origin_server_request_uri, abs_path);
841        origin_server_request_uri[abs_path_length] = '?';
842        strcpy(origin_server_request_uri + abs_path_length + 1, query);
843      }
844      HTTPExchange_set_request_uri(server_exchange, origin_server_request_uri);
845      free(origin_server_request_uri);
846    
847    for (const HTTPHeader * h = HTTPExchange_get_request_headers(client_exchange); h != NULL; h = h->next) {    for (const HTTPHeader * h = HTTPExchange_get_request_headers(client_exchange); h != NULL; h = h->next) {
848      if (strcasecmp(h->name, HTTP_TRAILER) == 0 || strcasecmp(h->name, HTTP_TRANSFER_ENCODING) == 0) {      if (strcasecmp(h->name, HTTP_TRAILER) == 0 || strcasecmp(h->name, HTTP_TRANSFER_ENCODING) == 0) {
849        /* do nothing: we want to keep this header */        /* do nothing: we want to keep this header */
# Line 845  Line 886 
886    
887    HTTPExchange_set_status_code(client_exchange, HTTPExchange_get_status_code(server_exchange));    HTTPExchange_set_status_code(client_exchange, HTTPExchange_get_status_code(server_exchange));
888    
889    if (HTTPExchange_response_has_body(server_exchange) && should_instrument_request(server_exchange)) {    if (HTTPExchange_response_has_body(server_exchange) && should_instrument_request(server_exchange, HTTPExchange_get_request_uri(client_exchange))) {
890      /* needs instrumentation */      /* needs instrumentation */
891      Stream * input_stream = Stream_new(0);      Stream * input_stream = Stream_new(0);
892      if (HTTPExchange_read_entire_response_entity_body(server_exchange, input_stream) != 0) {      if (HTTPExchange_read_entire_response_entity_body(server_exchange, input_stream) != 0) {
# Line 1240  Line 1281 
1281      fatal_command_line("--port: option must be 16 bits");      fatal_command_line("--port: option must be 16 bits");
1282    }    }
1283    
1284      /* check the document root exists and is a directory */
1285      struct stat buf;
1286      xstat(document_root, &buf);
1287      if (! S_ISDIR(buf.st_mode)) {
1288        fatal_command_line("--document-root: option must be a directory");
1289      }
1290    
1291    /* is this a shutdown? */    /* is this a shutdown? */
1292    if (shutdown) {    if (shutdown) {
1293  #ifdef __MINGW32__  #ifdef __MINGW32__
# Line 1249  Line 1297 
1297      }      }
1298  #endif  #endif
1299    
1300      /* INADDR_LOOPBACK */      HTTPConnection * connection = HTTPConnection_new_client(ip_address, numeric_port);
     HTTPConnection * connection = HTTPConnection_new_client("127.0.0.1", numeric_port);  
1301      if (connection == NULL) {      if (connection == NULL) {
1302        fatal("could not connect to server");        fatal("could not connect to server");
1303      }      }
# Line 1292  Line 1339 
1339      printf("Starting HTTP server on %s:%lu\n", ip_address, numeric_port);      printf("Starting HTTP server on %s:%lu\n", ip_address, numeric_port);
1340      fflush(stdout);      fflush(stdout);
1341    }    }
1342    
1343      /* set the IP address */
1344      if (! inet_aton(ip_address, &jscoverage_ip_address)) {
1345        fatal("invalid IP address: %s\n", ip_address);
1346      }
1347    
1348    HTTPServer_run(ip_address, (uint16_t) numeric_port, handler);    HTTPServer_run(ip_address, (uint16_t) numeric_port, handler);
1349    if (verbose) {    if (verbose) {
1350      printf("Stopping HTTP server\n");      printf("Stopping HTTP server\n");

Legend:
Removed from v.447  
changed lines
  Added in v.583

  ViewVC Help
Powered by ViewVC 1.1.24