Class: Nyara::Request

Inherits:
Object show all
Defined in:
lib/nyara/request.rb,
ext/request.c

Overview

Request and handler

Constant Summary collapse

FORM_METHODS =
%w[
POST
PUT
DELETE
PATCH
FORM_MEDIA_TYPES =
%w[
application/x-www-form-urlencoded
multipart/form-data

Instance Method Summary collapse

Instance Method Details

#acceptObject


169
170
171
172
# File 'ext/request.c', line 169

static VALUE request_accept(VALUE self) {
  P;
  return p->accept;
}

#accept_charsetObject


92
93
94
# File 'lib/nyara/request.rb', line 92

def accept_charset
  @accept_charset ||= Ext.parse_accept_value header['Accept-Charset']
end

#accept_encodingObject


96
97
98
# File 'lib/nyara/request.rb', line 96

def accept_encoding
  @accept_encoding ||= Ext.parse_accept_value header['Accept-Encoding']
end

#accept_languageObject


88
89
90
# File 'lib/nyara/request.rb', line 88

def accept_language
  @accept_language ||= Ext.parse_accept_value header['Accept-Language']
end

#bodyObject


210
211
212
213
214
215
216
# File 'ext/request.c', line 210

static VALUE request_body(VALUE self) {
  P;
  while (p->parse_state != PS_MESSAGE_COMPLETE) {
    rb_fiber_yield(1, &sym_reading);
  }
  return p->body;
}

179
180
181
182
# File 'ext/request.c', line 179

static VALUE request_cookie(VALUE self) {
  P;
  return p->cookie;
}

#domainObject


52
53
54
55
56
57
58
59
60
61
# File 'lib/nyara/request.rb', line 52

def domain
  @domain ||= begin
    r = header['Host']
    if r
      r.split(':', 2).first
    else
      Config['host'] || 'localhost'
    end
  end
end

#flashObject


197
198
199
200
# File 'ext/request.c', line 197

static VALUE request_flash(VALUE self) {
  P;
  return p->flash;
}

#flash=Object


202
203
204
205
206
207
208
# File 'ext/request.c', line 202

static VALUE request_flash_eq(VALUE self, VALUE flash) {
  P;
  if (p->flash != Qnil) {
    rb_raise(rb_eRuntimeError, "request.flash already initialized");
  }
  return p->flash = flash;
}

#form?Boolean

Returns:

  • (Boolean)

112
113
114
115
116
117
118
119
120
# File 'lib/nyara/request.rb', line 112

def form?
  if type = header['Content-Type']
    type = type[/[^;\s]+/]
    FORM_METHODS.include?(http_method) and
    FORM_MEDIA_TYPES.include?(type)
  else
    post?
  end
end

#formatObject


174
175
176
177
# File 'ext/request.c', line 174

static VALUE request_format(VALUE self) {
  P;
  return p->format == Qnil ? str_html : p->format;
}

#headerObject


144
145
146
147
# File 'ext/request.c', line 144

static VALUE request_header(VALUE self) {
  P;
  return p->header;
}

#host_with_portObject


73
74
75
76
77
78
79
80
81
82
# File 'lib/nyara/request.rb', line 73

def host_with_port
  header['Host'] || begin
    p = port
    if p == 80
      domain
    else
      "#{domain}:#{p}"
    end
  end
end

#http_methodObject


137
138
139
140
141
142
# File 'ext/request.c', line 137

static VALUE request_http_method(VALUE self) {
  P;
  // todo reduce allocation
  const char* str = http_method_str(p->method);
  return rb_enc_str_new(str, strlen(str), u8_encoding);
}

#inspectObject


142
143
144
145
146
147
# File 'lib/nyara/request.rb', line 142

def inspect
  "#<Nyara::Request%s>" %
    instance_variables.map { |iv|
      " #{iv}=#{instance_variable_get(iv).to_s}"
    }.join
end

#message_complete?Boolean

Returns:

  • (Boolean)

218
219
220
221
# File 'ext/request.c', line 218

static VALUE request_message_complete_p(VALUE self) {
  P;
  return (p->parse_state == PS_MESSAGE_COMPLETE) ? Qtrue : Qfalse;
}

#paramObject


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/nyara/request.rb', line 122

def param
  @param ||= begin
    q = query ? query.dup : ParamHash.new
    if form?
      b = body # read all the message
      case b
      when String
        ParamHash.parse_param q, body
      when Array
        b.each do |part|
          part.merge_into q
        end
      else
        # nil
      end
    end
    q
  end
end

#pathObject


154
155
156
157
# File 'ext/request.c', line 154

static VALUE request_path(VALUE self) {
  P;
  return p->path;
}

#path_with_queryObject


164
165
166
167
# File 'ext/request.c', line 164

static VALUE request_path_with_query(VALUE self) {
  P;
  return p->path_with_query;
}

#portObject


63
64
65
66
67
68
69
70
71
# File 'lib/nyara/request.rb', line 63

def port
  @port ||= begin
    r = header['Host']
    if r
      r = r.split(':', 2).last
    end
    r ? r.to_i : (Config['port'] || 80)
  end
end

#queryObject


159
160
161
162
# File 'ext/request.c', line 159

static VALUE request_query(VALUE self) {
  P;
  return p->query;
}

#response_content_typeObject


228
229
230
231
# File 'ext/request.c', line 228

static VALUE request_response_content_type(VALUE self) {
  P;
  return p->response_content_type;
}

#response_content_type=Object


233
234
235
236
237
# File 'ext/request.c', line 233

static VALUE request_response_content_type_eq(VALUE self, VALUE ct) {
  P;
  p->response_content_type = ct;
  return self;
}

#response_headerObject


239
240
241
242
# File 'ext/request.c', line 239

static VALUE request_response_header(VALUE self) {
  P;
  return p->response_header;
}

#response_header_extra_linesObject


244
245
246
247
# File 'ext/request.c', line 244

static VALUE request_response_header_extra_lines(VALUE self) {
  P;
  return p->response_header_extra_lines;
}

#schemeObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nyara/request.rb', line 33

def scheme
  @scheme ||= begin
    h = header
    if h['X-Forwarded-Ssl'] == 'on'
      'https'
    elsif s = h['X-Forwarded-Scheme']
      s
    elsif s = h['X-Forwarded-Proto']
      s.split(',')[0]
    else
      'http'
    end
  end
end

#scopeObject


149
150
151
152
# File 'ext/request.c', line 149

static VALUE request_scope(VALUE self) {
  P;
  return p->scope;
}

#sessionObject


184
185
186
187
# File 'ext/request.c', line 184

static VALUE request_session(VALUE self) {
  P;
  return p->session;
}

#session=Object


189
190
191
192
193
194
195
# File 'ext/request.c', line 189

static VALUE request_session_eq(VALUE self, VALUE session) {
  P;
  if (p->session != Qnil) {
    rb_raise(rb_eRuntimeError, "request.session already initialized");
  }
  return p->session = session;
}

#ssl?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/nyara/request.rb', line 48

def ssl?
  scheme == 'https'
end

#statusObject


223
224
225
226
# File 'ext/request.c', line 223

static VALUE request_status(VALUE self) {
  P;
  return INT2FIX(p->status);
}

#xhr?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/nyara/request.rb', line 84

def xhr?
  header["Requested-With"] == "XMLHttpRequest"
end