
  2002-11-09 (-q1)
    ・charsetの指定がないとき，Content-Typeヘッダにcharsetを付けないようにした。
    ・エラーが発生したときに，原因は不明だが，erubyが居座ってしまう。Apache
      のエラーログに大量に出力があるが，画面に表示した方がよさそうなので，
      CGIの場合は，常にエラーメッセージを表示するようにする。

  2002-11-10 (-q2)
    ・エラーメッセージを表示するときは，状態コードを500とした。

  2002-11-23 (-q3)
    ・ERuby.headers_outを追加。HTTPヘッダーフィールドを追加できるようにした。

  2010-06-22  HORIKAWA Hisashi
        * Ruby 1.8.7 + eruby 1.0.5 の組み合わせに対応。(-q4)

diff -Ncr eruby-1.0.5.orig/configure.rb eruby-1.0.5/configure.rb
*** eruby-1.0.5.orig/configure.rb	2003-02-10 12:18:10.000000000 +0900
--- eruby-1.0.5/configure.rb	2010-06-22 05:29:03.096889268 +0900
***************
*** 306,312 ****
    $LDFLAGS = "-link -incremental:no -pdb:none"
  end
  $LIBS = CONFIG["LIBS"]
! $XLDFLAGS = CONFIG["XLDFLAGS"]
  $XLDFLAGS.gsub!(/-L\./, "")
  if /mswin32/ !~ RUBY_PLATFORM
    $XLDFLAGS += " -L$(libdir)"
--- 306,312 ----
    $LDFLAGS = "-link -incremental:no -pdb:none"
  end
  $LIBS = CONFIG["LIBS"]
! $XLDFLAGS = CONFIG["XLDFLAGS"] || ""
  $XLDFLAGS.gsub!(/-L\./, "")
  if /mswin32/ !~ RUBY_PLATFORM
    $XLDFLAGS += " -L$(libdir)"
diff -Ncr eruby-1.0.5.orig/eruby.h eruby-1.0.5/eruby.h
*** eruby-1.0.5.orig/eruby.h	2003-12-24 00:11:54.000000000 +0900
--- eruby-1.0.5/eruby.h	2010-06-22 05:25:56.254299259 +0900
***************
*** 30,35 ****
--- 30,36 ----
  extern int eruby_noheader;
  extern VALUE eruby_charset;
  extern VALUE eruby_default_charset;
+ extern VALUE eruby_headers_out;
  #define ERUBY_CHARSET RSTRING(eruby_charset)->ptr
  
  const char *eruby_version();
diff -Ncr eruby-1.0.5.orig/eruby_lib.c eruby-1.0.5/eruby_lib.c
*** eruby-1.0.5.orig/eruby_lib.c	2003-07-29 12:42:56.000000000 +0900
--- eruby-1.0.5/eruby_lib.c	2010-06-22 05:25:56.256300492 +0900
***************
*** 50,55 ****
--- 50,56 ----
  int eruby_noheader = 0;
  VALUE eruby_charset;
  VALUE eruby_default_charset;
+ VALUE eruby_headers_out;
  
  #define ERUBY_BEGIN_DELIMITER "<%"
  #define ERUBY_END_DELIMITER "%>"
***************
*** 760,765 ****
--- 761,775 ----
      return Qnil;
  }
  
+ static VALUE eruby_get_headers_out(VALUE self)
+ {
+     if (NIL_P(eruby_headers_out)) {
+         eruby_headers_out = rb_ary_new();
+         rb_global_variable(&eruby_headers_out);
+     }
+     return eruby_headers_out;
+ }
+ 
  void eruby_init()
  {
      rb_define_virtual_variable("$NOHEADER", noheader_getter, noheader_setter);
***************
*** 775,780 ****
--- 785,791 ----
  			       eruby_set_default_charset, 1);
      rb_define_singleton_method(mERuby, "import", eruby_import, 1);
      rb_define_singleton_method(mERuby, "load", eruby_import, 1);
+     rb_define_singleton_method(mERuby, "headers_out", eruby_get_headers_out, 0);
  
      cERubyCompiler = rb_define_class_under(mERuby, "Compiler", rb_cObject);
      rb_define_singleton_method(cERubyCompiler, "new", eruby_compiler_s_new, 0);
***************
*** 795,800 ****
--- 806,813 ----
      rb_global_variable(&eruby_charset);
      rb_global_variable(&eruby_default_charset);
  
+     eruby_headers_out = Qnil;
+ 
      rb_provide("eruby");
  }
  
diff -Ncr eruby-1.0.5.orig/eruby_main.c eruby-1.0.5/eruby_main.c
*** eruby-1.0.5.orig/eruby_main.c	2003-12-24 00:10:54.000000000 +0900
--- eruby-1.0.5/eruby_main.c	2010-06-22 05:25:56.257298877 +0900
***************
*** 288,300 ****
      return s;
  }
  
! static void print_http_headers()
  {
!     char *tmp;
  
      if ((tmp = getenv("SERVER_PROTOCOL")) == NULL)
          tmp = "HTTP/1.0";
!     printf("%s 200 OK\r\n", tmp);
      if ((tmp = getenv("SERVER_SOFTWARE")) == NULL)
  	tmp = "unknown-server/0.0";
      printf("Server: %s\r\n", tmp);
--- 288,300 ----
      return s;
  }
  
! static void print_http_headers(const char* status)
  {
!     const char* tmp;
  
      if ((tmp = getenv("SERVER_PROTOCOL")) == NULL)
          tmp = "HTTP/1.0";
!     printf("%s %s\r\n", tmp, status);
      if ((tmp = getenv("SERVER_SOFTWARE")) == NULL)
  	tmp = "unknown-server/0.0";
      printf("Server: %s\r\n", tmp);
***************
*** 314,321 ****
  	if ((imgdir = getenv("SCRIPT_NAME")) == NULL)
  	    imgdir = "UNKNOWN_IMG_DIR";
          if (mode == MODE_NPHCGI)
!             print_http_headers();
          fprintf(out, "Content-Type: text/html\r\n");
          fprintf(out, "Content-Style-Type: text/css\r\n");
          fprintf(out, "\r\n");
  	fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n");
--- 314,322 ----
  	if ((imgdir = getenv("SCRIPT_NAME")) == NULL)
  	    imgdir = "UNKNOWN_IMG_DIR";
          if (mode == MODE_NPHCGI)
!             print_http_headers("500 Internal Server Error");
          fprintf(out, "Content-Type: text/html\r\n");
+         fprintf(out, "Status: 500 Internal Server Error\r\n");
          fprintf(out, "Content-Style-Type: text/css\r\n");
          fprintf(out, "\r\n");
  	fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n");
***************
*** 436,442 ****
  static void give_img_logo(int mode)
  {
      if (mode == MODE_NPHCGI)
! 	print_http_headers();
      printf("Content-Type: image/png\r\n\r\n");
      fwrite(eruby_logo_data, eruby_logo_size, 1, stdout);
  }
--- 437,443 ----
  static void give_img_logo(int mode)
  {
      if (mode == MODE_NPHCGI)
! 	print_http_headers("200 OK");
      printf("Content-Type: image/png\r\n\r\n");
      fwrite(eruby_logo_data, eruby_logo_size, 1, stdout);
  }
***************
*** 557,564 ****
      Init_stack(&stack_start);
      code = eruby_load(eruby_filename, 0, &state);
      if (state && !rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) {
! 	if (RTEST(ruby_debug) &&
! 	    (eruby_mode == MODE_CGI || eruby_mode == MODE_NPHCGI)) {
  	    error_print(stdout, state, 1, eruby_mode, code);
  	    eruby_exit(0);
  	}
--- 558,564 ----
      Init_stack(&stack_start);
      code = eruby_load(eruby_filename, 0, &state);
      if (state && !rb_obj_is_kind_of(ruby_errinfo, rb_eSystemExit)) {
! 	if (eruby_mode == MODE_CGI || eruby_mode == MODE_NPHCGI) {
  	    error_print(stdout, state, 1, eruby_mode, code);
  	    eruby_exit(0);
  	}
***************
*** 578,589 ****
      nout = RSTRING(rb_defout)->len;
  #endif
      if (!eruby_noheader &&
! 	(eruby_mode == MODE_CGI || eruby_mode == MODE_NPHCGI)) {
  	if (eruby_mode == MODE_NPHCGI)
! 	    print_http_headers();
  
! 	printf("Content-Type: text/html; charset=%s\r\n", ERUBY_CHARSET);
  	printf("Content-Length: %d\r\n", nout);
  	printf("\r\n");
      }
      fwrite(out, nout, 1, stdout);
--- 578,602 ----
      nout = RSTRING(rb_defout)->len;
  #endif
      if (!eruby_noheader &&
!        (eruby_mode == MODE_CGI || eruby_mode == MODE_NPHCGI)) {
  	if (eruby_mode == MODE_NPHCGI)
! 	    print_http_headers("200 OK");
  
!         if (ERUBY_CHARSET && ERUBY_CHARSET[0] != '\0')
!             printf("Content-Type: text/html; charset=%s\r\n", ERUBY_CHARSET);
!         else
!             printf("Content-Type: text/html\r\n");
  	printf("Content-Length: %d\r\n", nout);
+ 
+         if (!NIL_P(eruby_headers_out)) {
+             int i;
+             for (i = 0; i < RARRAY(eruby_headers_out)->len; i++) {
+                 VALUE s = rb_ary_entry(eruby_headers_out, i);
+                 Check_Type(s, T_STRING);
+                 printf("%s\r\n", STR2CSTR(s));
+             }
+         }
+ 
  	printf("\r\n");
      }
      fwrite(out, nout, 1, stdout);
