Class: FCGI

Inherits:
Object
  • Object
show all
Defined in:
lib/fcgi.rb,
ext/fcgi/fcgi.c

Overview

There is no C version of ‘each_cgi’ Note: for ruby-1.6.8 at least, the constants CGI_PARAMS/CGI_COOKIES are defined within module ‘CGI’, even if you have subclassed it

Defined Under Namespace

Classes: Error, Stream

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acceptObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'ext/fcgi/fcgi.c', line 80

static VALUE fcgi_s_accept(VALUE self)
{
  int status;
  FCGX_Request *req;
  rb_fdset_t readfds;

  req = ALLOC(FCGX_Request);

  status = FCGX_InitRequest(req,0,0);
  if (status != 0) {
    rb_raise(eFCGIError, "FCGX_Init() failed");
    return Qnil;
  }

  rb_fd_init(&readfds);
  rb_fd_set(req->listen_sock, &readfds);
  if (rb_thread_fd_select(readfds.maxfd, &readfds, NULL, NULL, NULL) < 1) {
    return Qnil;
  }

  status = FCGX_Accept_r(req);
  if (status >= 0) {
    fcgi_data *data;
    fcgi_stream_data *stream_data;
    char      **env;
    VALUE     obj,key, value;
    char      *pkey,*pvalue;
    int       flags, fd;

    /* Unset NONBLOCKING */
    fd = ((FCGX_Request*) req)->ipcFd;
    flags = fcntl(fd, F_GETFL);

    if (flags & O_NONBLOCK) {
       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
    }

    obj = Data_Make_Struct(self, fcgi_data, fcgi_mark, fcgi_free_req, data);
    data->req = req;
    data->in = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
    stream_data->stream = req->in;
    stream_data->req = obj;
    data->out = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
    stream_data->stream = req->out;
    stream_data->req = obj;
    data->err = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
    stream_data->stream = req->err;
    stream_data->req = obj;
    data->env = rb_hash_new();
    env = req->envp;
    for (; *env; env++) {
      int size = 0;
      pkey = *env;
      pvalue = pkey;
      while( *(pvalue++) != '=') size++;
      key   = rb_str_new(pkey, size);
      value = rb_str_new2(pvalue);
      OBJ_TAINT(key);
      OBJ_TAINT(value);
      rb_hash_aset(data->env, key, value);
    }

    return obj;
  } else {
    FCGX_Free(req, 1);
    free(req);
    return Qnil;
  }
}

.eachObject



150
151
152
153
154
155
156
157
158
# File 'ext/fcgi/fcgi.c', line 150

static VALUE fcgi_s_each(VALUE self)
{
  VALUE fcgi;

  while ((fcgi = fcgi_s_accept(self)) != Qnil) {
    rb_yield(fcgi);
  }
  return Qnil;
}

.each_cgi(*args) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/fcgi.rb', line 588

def self.each_cgi(*args)
  require 'cgi'

  eval(<<-EOS,TOPLEVEL_BINDING)
  class CGI
    public :env_table
    def self.remove_params
      if (const_defined?(:CGI_PARAMS))
        remove_const(:CGI_PARAMS)
        remove_const(:CGI_COOKIES)
      end
    end
  end # ::CGI class

  class FCGI
    class CGI < ::CGI
      def initialize(request, *args)
        ::CGI.remove_params
        @request = request
        super(*args)
        @args = *args
      end
      def args
        @args
      end
      def env_table
        @request.env
      end
      def stdinput
        @request.in
      end
      def stdoutput
        @request.out
      end
    end # FCGI::CGI class
  end # FCGI class
  EOS

  if FCGI::is_cgi?
    yield ::CGI.new(*args)
  else
    FCGI::each do |request|

      $stdout, $stderr = request.out, request.err

      yield CGI.new(request, *args)

      request.finish
    end
  end
end

.each_requestObject



150
151
152
153
154
155
156
157
158
# File 'ext/fcgi/fcgi.c', line 150

static VALUE fcgi_s_each(VALUE self)
{
  VALUE fcgi;

  while ((fcgi = fcgi_s_accept(self)) != Qnil) {
    rb_yield(fcgi);
  }
  return Qnil;
}

.is_cgi?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
# File 'ext/fcgi/fcgi.c', line 160

static VALUE fcgi_s_iscgi(VALUE self)
{
  if (FCGX_IsCGI()) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

Instance Method Details

#envObject



193
194
195
196
197
198
199
# File 'ext/fcgi/fcgi.c', line 193

static VALUE fcgi_env(VALUE self)
{
  fcgi_data *data;

  Data_Get_Struct(self, fcgi_data, data);
  return data->env;
}

#errObject



185
186
187
188
189
190
191
# File 'ext/fcgi/fcgi.c', line 185

static VALUE fcgi_err(VALUE self)
{
  fcgi_data *data;

  Data_Get_Struct(self, fcgi_data, data);
  return data->err;
}

#finishObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/fcgi/fcgi.c', line 201

static VALUE fcgi_finish(VALUE self)
{
  fcgi_data *data;
  fcgi_stream_data *stream_data;

  Data_Get_Struct(self, fcgi_data, data);

  if (Qnil != data->in) {
    Data_Get_Struct(data->in, fcgi_stream_data, stream_data);
    stream_data->req = Qnil; stream_data->stream = NULL;
  }
  if (Qnil != data->out) {
    Data_Get_Struct(data->out, fcgi_stream_data, stream_data);
    stream_data->req = Qnil; stream_data->stream = NULL;
  }
  if (Qnil != data->err) {
    Data_Get_Struct(data->err, fcgi_stream_data, stream_data);
    stream_data->req = Qnil; stream_data->stream = NULL;
  }

  data->in = data->out = data->err = Qnil;
  FCGX_Finish_r(data->req);

  return Qtrue;
}

#inObject



169
170
171
172
173
174
175
# File 'ext/fcgi/fcgi.c', line 169

static VALUE fcgi_in(VALUE self)
{
  fcgi_data *data;

  Data_Get_Struct(self, fcgi_data, data);
  return data->in;
}

#outObject



177
178
179
180
181
182
183
# File 'ext/fcgi/fcgi.c', line 177

static VALUE fcgi_out(VALUE self)
{
  fcgi_data *data;

  Data_Get_Struct(self, fcgi_data, data);
  return data->out;
}