ruby-lsapi 1.8 → 1.9

Sign up to get free protection for your applications and to get access to all the features.
data/ext/lsapi/extconf.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  require 'mkmf'
2
2
  dir_config( 'lsapi' )
3
+ if ( have_library( "socket" ))
4
+ have_library( "nsl" )
5
+ end
3
6
  create_makefile( "lsapi" )
data/ext/lsapi/lsapilib.c CHANGED
@@ -513,9 +513,9 @@ static int readReq( LSAPI_Request * pReq )
513
513
  if ( packetLen > LSAPI_MAX_HEADER_LEN )
514
514
  return -1;
515
515
 
516
- if ( packetLen > pReq->m_reqBufSize )
516
+ if ( packetLen + 1024 > pReq->m_reqBufSize )
517
517
  {
518
- if ( allocateBuf( pReq, packetLen ) == -1 )
518
+ if ( allocateBuf( pReq, packetLen + 1024 ) == -1 )
519
519
  return -1;
520
520
  }
521
521
  while( packetLen > pReq->m_bufRead )
@@ -717,6 +717,85 @@ char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex )
717
717
  return pReq->m_pHttpHeader + off;
718
718
  }
719
719
 
720
+ static int readBodyToReqBuf( LSAPI_Request * pReq )
721
+ {
722
+ int bodyLeft;
723
+ int len = pReq->m_bufRead - pReq->m_bufProcessed;
724
+ if ( len > 0 )
725
+ return len;
726
+ pReq->m_bufRead = pReq->m_bufProcessed = pReq->m_pHeader->m_pktHeader.m_packetLen.m_iLen;
727
+
728
+ bodyLeft = pReq->m_pHeader->m_reqBodyLen - pReq->m_reqBodyRead;
729
+ len = pReq->m_reqBufSize - pReq->m_bufRead;
730
+ if ( len < 0 )
731
+ return -1;
732
+ if ( len > bodyLeft )
733
+ len = bodyLeft;
734
+
735
+ len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, len );
736
+ if ( len > 0 )
737
+ pReq->m_bufRead += len;
738
+ return len;
739
+ }
740
+
741
+
742
+ int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq )
743
+ {
744
+ if ( pReq->m_bufProcessed >= pReq->m_bufRead )
745
+ {
746
+ if ( readBodyToReqBuf( pReq ) <= 0 )
747
+ return EOF;
748
+ }
749
+ ++pReq->m_reqBodyRead;
750
+ return (unsigned char)*(pReq->m_pReqBuf + pReq->m_bufProcessed++);
751
+ }
752
+
753
+
754
+
755
+ int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, int bufLen, int *getLF )
756
+ {
757
+ int len;
758
+ int left;
759
+ char * pBufEnd = pBuf + bufLen - 1;
760
+ char * pBufCur = pBuf;
761
+ if (!pReq || ( !pBuf )||(bufLen < 0 )|| !getLF )
762
+ return -1;
763
+ *getLF = 0;
764
+ while( (left = pBufEnd - pBufCur ) > 0 )
765
+ {
766
+
767
+ len = pReq->m_bufRead - pReq->m_bufProcessed;
768
+ if ( len <= 0 )
769
+ {
770
+ if ( (len = readBodyToReqBuf( pReq )) <= 0 )
771
+ {
772
+ *getLF = 1;
773
+ break;
774
+ }
775
+ }
776
+ if ( len > left )
777
+ len = left;
778
+ char * pCur = pReq->m_pReqBuf + pReq->m_bufProcessed;
779
+ char * p = memchr( pCur, '\n', len );
780
+ if ( p )
781
+ len = p - pCur + 1;
782
+ memmove( pBufCur, pCur, len );
783
+ pBufCur += len;
784
+ pReq->m_bufProcessed += len;
785
+
786
+ pReq->m_reqBodyRead += len;
787
+
788
+ if ( p )
789
+ {
790
+ *getLF = 1;
791
+ break;
792
+ }
793
+ }
794
+ *pBufCur = 0;
795
+
796
+ return pBufCur - pBuf;
797
+ }
798
+
720
799
 
721
800
  int LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, int bufLen )
722
801
  {
@@ -755,7 +834,8 @@ int LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, int bufLen )
755
834
  bufLen -= len;
756
835
  }
757
836
  else if ( len < 0 )
758
- return (total)?total:-1;
837
+ if ( !total)
838
+ return -1;
759
839
  }
760
840
  pReq->m_reqBodyRead += total;
761
841
  return total;
data/ext/lsapi/lsapilib.h CHANGED
@@ -151,6 +151,10 @@ char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name );
151
151
 
152
152
  int LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, int len );
153
153
 
154
+ int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq );
155
+
156
+ int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, int bufLen, int *getLF );
157
+
154
158
 
155
159
  int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq );
156
160
 
@@ -262,6 +266,14 @@ static inline int LSAPI_GetReqBodyRemain()
262
266
  static inline int LSAPI_ReadReqBody( char * pBuf, int len )
263
267
  { return LSAPI_ReadReqBody_r( &g_req, pBuf, len ); }
264
268
 
269
+ static inline int LSAPI_ReqBodyGetChar()
270
+ { return LSAPI_ReqBodyGetChar_r( &g_req ); }
271
+
272
+ static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
273
+ { return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF ); }
274
+
275
+
276
+
265
277
  static inline int LSAPI_FinalizeRespHeaders(void)
266
278
  { return LSAPI_FinalizeRespHeaders_r( &g_req ); }
267
279
 
data/ext/lsapi/lsruby.c CHANGED
@@ -74,16 +74,76 @@ static void lsapi_free_data( lsapi_data * data )
74
74
  free( data );
75
75
  }
76
76
 
77
- static int add_env( const char * pKey, int keyLen, const char * pValue, int valLen,
77
+ static int add_env_rails( const char * pKey, int keyLen, const char * pValue, int valLen,
78
78
  void * arg )
79
79
  {
80
+ char * p;
81
+ int len;
82
+ //Fixup some environment variables for rails
83
+ switch( *pKey )
84
+ {
85
+ case 'Q':
86
+ if ( strcmp( pKey, "QUERY_STRING" ) == 0 )
87
+ {
88
+ if ( !*pValue )
89
+ return 1;
90
+ }
91
+ break;
92
+ case 'R':
93
+ if (( *(pKey+8) == 'U' )&&( strcmp( pKey, "REQUEST_URI" ) == 0 ))
94
+ {
95
+ p = strchr( pValue, '?' );
96
+ if ( p )
97
+ {
98
+ len = valLen - ( p - pValue ) - 1;
99
+ valLen = p - pValue;
100
+ *p++ = 0;
101
+ }
102
+ else
103
+ {
104
+ p = "";
105
+ len = 0;
106
+ }
107
+ rb_hash_aset( lsapi_env,rb_tainted_str_new("QUERY_STRING", 12),
108
+ rb_tainted_str_new(p, len));
109
+ rb_hash_aset( lsapi_env,rb_tainted_str_new("PATH_INFO", 9),
110
+ rb_tainted_str_new(pValue, valLen));
111
+ }
112
+ break;
113
+ case 'S':
114
+ if ( strcmp( pKey, "SCRIPT_NAME" ) == 0 )
115
+ {
116
+ pValue = "/";
117
+ valLen = 1;
118
+ }
119
+ break;
120
+ case 'P':
121
+ if ( strcmp( pKey, "PATH_INFO" ) == 0 )
122
+ return 1;
123
+ default:
124
+ break;
125
+ }
126
+
80
127
  //lsapi_ruby_setenv(pKey, pValue );
81
- rb_hash_aset( lsapi_env,rb_tainted_str_new2(pKey),
82
- rb_tainted_str_new2(pValue));
128
+
129
+ rb_hash_aset( lsapi_env,rb_tainted_str_new(pKey, keyLen),
130
+ rb_tainted_str_new(pValue, valLen));
83
131
  return 1;
84
132
  }
85
133
 
134
+ static int add_env_no_fix( const char * pKey, int keyLen, const char * pValue, int valLen,
135
+ void * arg )
136
+ {
137
+ rb_hash_aset( lsapi_env,rb_tainted_str_new(pKey, keyLen),
138
+ rb_tainted_str_new(pValue, valLen));
139
+ return 1;
140
+ }
86
141
 
142
+ typedef int (*fn_add_env)( const char * pKey, int keyLen, const char * pValue, int valLen,
143
+ void * arg );
144
+
145
+ fn_add_env s_fn_add_env = add_env_no_fix;
146
+
87
147
  static void clear_env()
88
148
  {
89
149
  //rb_funcall( lsapi_env, rb_intern( "clear" ), 0 );
@@ -92,11 +152,10 @@ static void clear_env()
92
152
 
93
153
  static void setup_cgi_env( lsapi_data * data )
94
154
  {
95
-
96
155
  clear_env();
97
156
 
98
- LSAPI_ForeachHeader_r( data->req, add_env, data );
99
- LSAPI_ForeachEnv_r( data->req, add_env, data );
157
+ LSAPI_ForeachHeader_r( data->req, s_fn_add_env, data );
158
+ LSAPI_ForeachEnv_r( data->req, s_fn_add_env, data );
100
159
 
101
160
  }
102
161
 
@@ -510,11 +569,37 @@ static VALUE lsapi_flush( VALUE self )
510
569
 
511
570
  static VALUE lsapi_getc( VALUE self )
512
571
  {
513
- char ch;
572
+ int ch;
514
573
  //lsapi_data *data;
515
574
  //Data_Get_Struct(self,lsapi_data, data);
516
- LSAPI_ReadReqBody_r( &g_req, &ch, 1 );
517
- return INT2NUM( (int)ch );
575
+ if (rb_safe_level() >= 4 && !OBJ_TAINTED(self))
576
+ {
577
+ rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
578
+ }
579
+ ch = LSAPI_ReqBodyGetChar_r( &g_req );
580
+ return INT2NUM( ch );
581
+ }
582
+
583
+ static VALUE lsapi_gets( VALUE self )
584
+ {
585
+ VALUE str;
586
+ int getLF = 0;
587
+ char buff[4096];
588
+ int len;
589
+ if (rb_safe_level() >= 4 && !OBJ_TAINTED(self))
590
+ {
591
+ rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
592
+ }
593
+ str = rb_str_buf_new( 1024 );
594
+ OBJ_TAINT(str);
595
+ while( !getLF )
596
+ {
597
+ len = LSAPI_ReqBodyGetLine_r( &g_req, buff, 4096, &getLF );
598
+ if ( len > 0 )
599
+ rb_str_buf_cat( str, buff, len );
600
+
601
+ }
602
+ return str;
518
603
  }
519
604
 
520
605
 
@@ -614,7 +699,7 @@ void Init_lsapi()
614
699
  if ( p )
615
700
  {
616
701
  s_children = atoi( p );
617
- unsetenv( "LSAPI_CHILDREN" );
702
+ ruby_unsetenv( "LSAPI_CHILDREN" );
618
703
  }
619
704
 
620
705
  p = getenv( "LSAPI_MAX_REQS" );
@@ -623,7 +708,7 @@ void Init_lsapi()
623
708
  n = atoi( p );
624
709
  if ( n > 0 )
625
710
  s_max_reqs = n;
626
- unsetenv( "LSAPI_MAX_REQS" );
711
+ ruby_unsetenv( "LSAPI_MAX_REQS" );
627
712
  }
628
713
 
629
714
  p = getenv( "LSAPI_MAX_IDLE" );
@@ -631,21 +716,23 @@ void Init_lsapi()
631
716
  {
632
717
  n = atoi( p );
633
718
  s_max_idle_secs = n;
634
- unsetenv( "LSAPI_MAX_IDLE" );
719
+ ruby_unsetenv( "LSAPI_MAX_IDLE" );
635
720
  }
636
721
 
637
722
  p = getenv( "LSAPI_PGRP_MAX_IDLE" );
638
723
  if ( p )
639
724
  {
640
725
  s_max_grp_idle_secs = atoi( p );
641
- unsetenv( "LSAPI_PGRP_MAX_IDLE" );
726
+ ruby_unsetenv( "LSAPI_PGRP_MAX_IDLE" );
642
727
  }
643
728
 
644
729
  if ( !getenv( "LSAPI_PPID_NO_CHECK" ) )
645
730
  s_ppid = getppid();
646
731
  else
647
- unsetenv( "LSAPI_PPID_NO_CHECK" );
648
-
732
+ ruby_unsetenv( "LSAPI_PPID_NO_CHECK" );
733
+ if ( getenv( "RAILS_ROOT" )||getenv( "RAILS_ENV" ) )
734
+ s_fn_add_env = add_env_rails;
735
+
649
736
  orig_stdin = rb_stdin;
650
737
  orig_stdout = rb_stdout;
651
738
  #if RUBY_VERSION_CODE < 180
@@ -657,8 +744,6 @@ void Init_lsapi()
657
744
  //Do not need those environments after initialization
658
745
  remove_env = rb_str_new( "RAILS_ROOT", 10 );
659
746
  rb_funcall( env_copy, rb_intern( "delete" ), 1, remove_env );
660
- remove_env = rb_str_new( "RAILS_ENV", 9 );
661
- rb_funcall( env_copy, rb_intern( "delete" ), 1, remove_env );
662
747
 
663
748
  lsapi_env = rb_hash_new();
664
749
  //redefine ENV using a hash table, should be faster than char **environment
@@ -685,7 +770,7 @@ void Init_lsapi()
685
770
  rb_define_method(cLSAPI, "flush", lsapi_flush, 0);
686
771
  rb_define_method(cLSAPI, "getc", lsapi_getc, 0);
687
772
  //rb_define_method(cLSAPI, "ungetc", lsapi_ungetc, 1);
688
- //rb_define_method(cLSAPI, "gets", lsapi_gets, 0);
773
+ rb_define_method(cLSAPI, "gets", lsapi_gets, 0);
689
774
  rb_define_method(cLSAPI, "read", lsapi_read, -1);
690
775
  rb_define_method(cLSAPI, "eof", lsapi_eof, 0);
691
776
  rb_define_method(cLSAPI, "eof?", lsapi_eof, 0);
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ruby-lsapi
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.8"
7
- date: 2006-08-22 00:00:00 -04:00
6
+ version: "1.9"
7
+ date: 2006-09-25 00:00:00 -04:00
8
8
  summary: A ruby extension for fast communication with LiteSpeed Web Server.
9
9
  require_paths:
10
10
  - lib