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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
# File 'ext/fcgi/fcgi.c', line 61

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

  req = ALLOC(FCGX_Request);

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

  FD_ZERO(&readfds);
  FD_SET(req->listen_sock, &readfds);
  if (rb_thread_select(req->listen_sock+1, &readfds, NULL, NULL, NULL) < 1) {
    return Qnil;
  }

  status = FCGX_Accept_r(req);
  if (status >= 0) {
    fcgi_data *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_Wrap_Struct(cFCGIStream, 0, 0, req->in);
    data->out = Data_Wrap_Struct(cFCGIStream, 0, 0, req->out);
    data->err = Data_Wrap_Struct(cFCGIStream, 0, 0, req->err);
    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 {
    return Qnil;
  }
}

.eachObject



122
123
124
125
126
127
128
129
130
# File 'ext/fcgi/fcgi.c', line 122

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
639
# 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
    exit_requested = false
    FCGI::each do |request|
      
      $stdout, $stderr = request.out, request.err
      
      yield CGI.new(request, *args)
      
      request.finish
    end
  end
end

.each_requestObject



122
123
124
125
126
127
128
129
130
# File 'ext/fcgi/fcgi.c', line 122

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)


132
133
134
135
136
137
138
139
# File 'ext/fcgi/fcgi.c', line 132

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

Instance Method Details

#envObject



165
166
167
168
169
170
171
# File 'ext/fcgi/fcgi.c', line 165

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

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

#errObject



157
158
159
160
161
162
163
# File 'ext/fcgi/fcgi.c', line 157

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

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

#finishObject



173
174
175
176
177
178
179
180
181
182
# File 'ext/fcgi/fcgi.c', line 173

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

  Data_Get_Struct(self, fcgi_data, data);

  FCGX_Finish_r(data->req);

  return Qtrue;
}

#inObject



141
142
143
144
145
146
147
# File 'ext/fcgi/fcgi.c', line 141

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

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

#outObject



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

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

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