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



52
53
54
55
56
57
58
59
60
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
# File 'ext/fcgi/fcgi.c', line 52

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



113
114
115
116
117
118
119
120
121
# File 'ext/fcgi/fcgi.c', line 113

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



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
# File 'lib/fcgi.rb', line 567

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 {|request|
      $stdout, $stderr = request.out, request.err

      yield CGI.new(request, *args)
      
      request.finish
    }
  end
end

.each_requestObject



113
114
115
116
117
118
119
120
121
# File 'ext/fcgi/fcgi.c', line 113

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)


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

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

Instance Method Details

#envObject



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

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

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

#errObject



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

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

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

#finishObject



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

static VALUE fcgi_finish(VALUE self)
{
  fcgi_data *data;
  
  Data_Get_Struct(self, fcgi_data, data);
  
  FCGX_Finish_r(data->req);
  
  return Qtrue;
}

#inObject



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

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

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

#outObject



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

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

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