Class: Agoo::Response

Inherits:
Object
  • Object
show all
Defined in:
ext/agoo/response.c,
ext/agoo/response.c

Overview

A response passed to a handler that responds to the on_request method. The expected response is modified by the handler before returning.

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

call-seq: [](key)

Gets a header element associated with the key.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/agoo/response.c', line 179

static VALUE
head_get(VALUE self, VALUE key) {
    Response	res = (Response)DATA_PTR(self);
    Header	h;
    const char	*ks = StringValuePtr(key);
    int		klen = (int)RSTRING_LEN(key);
    
    for (h = res->headers; NULL != h; h = h->next) {
	if (0 == strncasecmp(h->text, ks, klen) && klen + 1 < h->len && ':' == h->text[klen]) {
	    return rb_str_new(h->text + klen + 2, h->len - klen - 4);
	}
    }
    return Qnil;
}

#[]=(key, val) ⇒ Object

call-seq: []=(key, value)

Sets a header element with the key and value provided.



200
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'ext/agoo/response.c', line 200

static VALUE
head_set(VALUE self, VALUE key, VALUE val) {
    Response	res = (Response)DATA_PTR(self);
    Header	h;
    Header	prev = NULL;
    const char	*ks = StringValuePtr(key);
    const char	*vs;
    int		klen = (int)RSTRING_LEN(key);
    int		vlen;
    int		hlen;

    for (h = res->headers; NULL != h; h = h->next) {
	if (0 == strncasecmp(h->text, ks, klen) && klen + 1 < h->len && ':' == h->text[klen]) {
	    if (NULL == prev) {
		res->headers = h->next;
	    } else {
		prev->next = h->next;
	    }
	    DEBUG_FREE(mem_header, h);
	    xfree(h);
	    break;
	}
	prev = h;
    }
    if (T_STRING != rb_type(val)) {
	val = rb_funcall(val, rb_intern("to_s"), 0);
    }
    vs = StringValuePtr(val);
    vlen = (int)RSTRING_LEN(val);

    if (the_server.pedantic) {
	struct _Err	err = ERR_INIT;

	if (ERR_OK != http_header_ok(&err, ks, klen, vs, vlen)) {
	    rb_raise(rb_eArgError, "%s", err.msg);
	}
    }
    hlen = klen + vlen + 4;
    h = (Header)ALLOC_N(char, sizeof(struct _Header) - 8 + hlen + 1);
    DEBUG_ALLOC(mem_header, h)

    h->next = NULL;
    h->len = hlen;
    strncpy(h->text, ks, klen);
    strcpy(h->text + klen, ": ");
    strncpy(h->text + klen + 2, vs, vlen);
    strcpy(h->text + klen + 2 + vlen, "\r\n");
    if (NULL == res->headers) {
	res->headers = h;
    } else {
	for (prev = res->headers; NULL != prev->next; prev = prev->next) {
	}
	prev->next = h;
    }
    return Qnil;
}

#bodyObject

call-seq: body()

Gets the HTTP body for the response.



106
107
108
109
110
111
112
113
114
# File 'ext/agoo/response.c', line 106

static VALUE
body_get(VALUE self) {
    Response	res = (Response)DATA_PTR(self);

    if (NULL == res->body) {
	return Qnil;
    }
    return rb_str_new(res->body, res->blen);
}

#body=(val) ⇒ Object

call-seq: body=(str)

Sets the HTTP body for the response.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/agoo/response.c', line 129

static VALUE
body_set(VALUE self, VALUE val) {
    Response	res = (Response)DATA_PTR(self);

    if (T_STRING == rb_type(val)) {
	res->body = strdup(StringValuePtr(val));
	DEBUG_ALLOC(mem_res_body, res->body)
	res->blen = (int)RSTRING_LEN(val);
    } else {
	rb_raise(rb_eArgError, "Expected a string");
	// TBD use Oj to encode val
    }
    return Qnil;
}

#codeObject

call-seq: code()

Gets the HTTP status code for the response.



150
151
152
153
# File 'ext/agoo/response.c', line 150

static VALUE
code_get(VALUE self) {
    return INT2NUM(((Response)DATA_PTR(self))->code);
}

#code=(val) ⇒ Object

call-seq: code=(value)

Sets the HTTP status code for the response.



161
162
163
164
165
166
167
168
169
170
171
# File 'ext/agoo/response.c', line 161

static VALUE
code_set(VALUE self, VALUE val) {
    int	code = NUM2INT(val);

    if (100 <= code && code < 600) {
	((Response)DATA_PTR(self))->code = code;
    } else {
	rb_raise(rb_eArgError, "%d is not a valid HTTP status code.", code);
    }
    return Qnil;
}

#contentObject

call-seq: body()

Gets the HTTP body for the response.



106
107
108
109
110
111
112
113
114
# File 'ext/agoo/response.c', line 106

static VALUE
body_get(VALUE self) {
    Response	res = (Response)DATA_PTR(self);

    if (NULL == res->body) {
	return Qnil;
    }
    return rb_str_new(res->body, res->blen);
}

#content=(val) ⇒ Object

call-seq: body=(str)

Sets the HTTP body for the response.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/agoo/response.c', line 129

static VALUE
body_set(VALUE self, VALUE val) {
    Response	res = (Response)DATA_PTR(self);

    if (T_STRING == rb_type(val)) {
	res->body = strdup(StringValuePtr(val));
	DEBUG_ALLOC(mem_res_body, res->body)
	res->blen = (int)RSTRING_LEN(val);
    } else {
	rb_raise(rb_eArgError, "Expected a string");
	// TBD use Oj to encode val
    }
    return Qnil;
}

#to_sObject

call-seq: to_s()

Returns a string representation of the response.



81
82
83
84
85
86
87
88
89
90
91
# File 'ext/agoo/response.c', line 81

static VALUE
to_s(VALUE self) {
    Response	res = (Response)DATA_PTR(self);
    int		len = response_len(res);
    char	*s = ALLOC_N(char, len + 1);

    DEBUG_ALLOC(mem_to_s, s)
    response_fill(res, s);
    
    return rb_str_new(s, len);
}