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.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/agoo/response.c', line 172

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.



193
194
195
196
197
198
199
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
# File 'ext/agoo/response.c', line 193

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;
	    }
	    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 (res->server->pedantic) {
	http_header_ok(ks, klen, vs, vlen);
    }
    hlen = klen + vlen + 4;
    h = (Header)ALLOC_N(char, sizeof(struct _Header) - 8 + hlen + 1);
    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.



101
102
103
104
105
106
107
108
109
# File 'ext/agoo/response.c', line 101

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.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/agoo/response.c', line 124

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));
	res->blen = (int)RSTRING_LEN(val);
    } else {
	// TBD use Oj
    }
    return Qnil;
}

#codeObject

call-seq: code()

Gets the HTTP status code for the response.



143
144
145
146
# File 'ext/agoo/response.c', line 143

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.



154
155
156
157
158
159
160
161
162
163
164
# File 'ext/agoo/response.c', line 154

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.



101
102
103
104
105
106
107
108
109
# File 'ext/agoo/response.c', line 101

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.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/agoo/response.c', line 124

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));
	res->blen = (int)RSTRING_LEN(val);
    } else {
	// TBD use Oj
    }
    return Qnil;
}

#to_sObject

call-seq: to_s()

Returns a string representation of the response.



77
78
79
80
81
82
83
84
85
86
# File 'ext/agoo/response.c', line 77

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

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