/[jscoverage]/tags/jscoverage-0.4/jscoverage-server.c
ViewVC logotype

Diff of /tags/jscoverage-0.4/jscoverage-server.c

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

revision 125 by siliconforks, Mon Jun 2 17:52:38 2008 UTC revision 177 by siliconforks, Sat Sep 20 23:30:26 2008 UTC
# Line 29  Line 29 
29  #include <pthread.h>  #include <pthread.h>
30  #endif  #endif
31    
32    #include "global.h"
33  #include "http-server.h"  #include "http-server.h"
34  #include "instrument-js.h"  #include "instrument-js.h"
35  #include "resource-manager.h"  #include "resource-manager.h"
36  #include "stream.h"  #include "stream.h"
37  #include "util.h"  #include "util.h"
38    
39    const char * jscoverage_encoding = "ISO-8859-1";
40    
41  typedef struct SourceCache {  typedef struct SourceCache {
42    char * url;    char * url;
43    Stream * source;    Stream * source;
# Line 526  Line 529 
529    
530      mkdir_if_necessary(report_directory);      mkdir_if_necessary(report_directory);
531      char * path = make_path(report_directory, "jscoverage.json");      char * path = make_path(report_directory, "jscoverage.json");
532      FILE * f = fopen(path, "r");  
533      if (f != NULL) {      /* check if the JSON file exists */
534        struct stat buf;
535        if (stat(path, &buf) == 0) {
536        /* it exists: merge */        /* it exists: merge */
537        result = merge(coverage, f);        FILE * f = fopen(path, "r");
538        if (fclose(f) == EOF) {        if (f == NULL) {
539          result = 1;          result = 1;
540        }        }
541          else {
542            result = merge(coverage, f);
543            if (fclose(f) == EOF) {
544              result = 1;
545            }
546          }
547        if (result != 0) {        if (result != 0) {
548          free(path);          free(path);
549          Coverage_delete(coverage);          Coverage_delete(coverage);
# Line 552  Line 563 
563      /* copy other files */      /* copy other files */
564      jscoverage_copy_resources(report_directory);      jscoverage_copy_resources(report_directory);
565      path = make_path(report_directory, "jscoverage.js");      path = make_path(report_directory, "jscoverage.js");
566      f = fopen(path, "ab");      FILE * f = fopen(path, "ab");
567      free(path);      free(path);
568      if (f == NULL) {      if (f == NULL) {
569        send_response(exchange, 500, "Could not write to file: jscoverage.js\n");        send_response(exchange, 500, "Could not write to file: jscoverage.js\n");
# Line 608  Line 619 
619    }    }
620  }  }
621    
622  static void instrument_js(const char * id, Stream * input_stream, Stream * output_stream) {  static void instrument_js(const char * id, const char * encoding, Stream * input_stream, Stream * output_stream) {
623    LOCK(&javascript_mutex);    LOCK(&javascript_mutex);
624    jscoverage_instrument_js(id, input_stream, output_stream);    jscoverage_instrument_js(id, encoding, input_stream, output_stream);
625    UNLOCK(&javascript_mutex);    UNLOCK(&javascript_mutex);
626    
627    const struct Resource * resource = get_resource("report.js");    const struct Resource * resource = get_resource("report.js");
# Line 733  Line 744 
744    
745      const char * request_uri = HTTPExchange_get_request_uri(client_exchange);      const char * request_uri = HTTPExchange_get_request_uri(client_exchange);
746      Stream * output_stream = Stream_new(0);      Stream * output_stream = Stream_new(0);
747      instrument_js(request_uri, input_stream, output_stream);      char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(server_exchange));
748        if (encoding == NULL) {
749          instrument_js(request_uri, jscoverage_encoding, input_stream, output_stream);
750        }
751        else {
752          instrument_js(request_uri, encoding, input_stream, output_stream);
753          free(encoding);
754        }
755    
756      /* send the headers to the client */      /* send the headers to the client */
757      for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) {      for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) {
# Line 875  Line 893 
893    
894      const char * content_type = get_content_type(filesystem_path);      const char * content_type = get_content_type(filesystem_path);
895      HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);      HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);
896      const char * request_uri = HTTPExchange_get_request_uri(exchange);      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {
     if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(request_uri)) {  
897        Stream * input_stream = Stream_new(0);        Stream * input_stream = Stream_new(0);
898        Stream * output_stream = Stream_new(0);        Stream * output_stream = Stream_new(0);
899    
900        Stream_write_file_contents(input_stream, f);        Stream_write_file_contents(input_stream, f);
901    
902        instrument_js(request_uri, input_stream, output_stream);        instrument_js(abs_path, jscoverage_encoding, input_stream, output_stream);
903    
904        if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) {        if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) {
905          HTTPServer_log_err("Warning: error writing to client\n");          HTTPServer_log_err("Warning: error writing to client\n");
# Line 968  Line 985 
985        document_root = argv[i] + 16;        document_root = argv[i] + 16;
986      }      }
987    
988        else if (strcmp(argv[i], "--encoding") == 0) {
989          i++;
990          if (i == argc) {
991            fatal("--encoding: option requires an argument");
992          }
993          jscoverage_encoding = argv[i];
994        }
995        else if (strncmp(argv[i], "--encoding=", 11) == 0) {
996          jscoverage_encoding = argv[i] + 11;
997        }
998    
999      else if (strcmp(argv[i], "--ip-address") == 0) {      else if (strcmp(argv[i], "--ip-address") == 0) {
1000        i++;        i++;
1001        if (i == argc) {        if (i == argc) {
# Line 1034  Line 1062 
1062  #ifdef __MINGW32__  #ifdef __MINGW32__
1063      WSADATA data;      WSADATA data;
1064      if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {      if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
1065        fatal("Could not start Winsock");        fatal("could not start Winsock");
1066      }      }
1067  #endif  #endif
1068    

Legend:
Removed from v.125  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC 1.1.24