Class: Agoo::Response

Inherits:
Object
  • Object
show all
Defined in:
ext/agoo/rresponse.c,
ext/agoo/rresponse.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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/agoo/rresponse.c', line 163

static VALUE
head_get(VALUE self, VALUE key) {
    agooResponse	res = (agooResponse)DATA_PTR(self);
    agooHeader		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.



184
185
186
187
188
189
190
191
192
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
# File 'ext/agoo/rresponse.c', line 184

static VALUE
head_set(VALUE self, VALUE key, VALUE val) {
    agooResponse	res = (agooResponse)DATA_PTR(self);
    agooHeader		h;
    agooHeader		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;
	    }
	    AGOO_FREED(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 (agoo_server.pedantic) {
	struct _agooErr	err = AGOO_ERR_INIT;

	if (AGOO_ERR_OK != agoo_http_header_ok(&err, ks, klen, vs, vlen)) {
	    rb_raise(rb_eArgError, "%s", err.msg);
	}
    }
    hlen = klen + vlen + 4;
    h = (agooHeader)AGOO_MALLOC(sizeof(struct _agooHeader) - 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.



89
90
91
92
93
94
95
96
97
# File 'ext/agoo/rresponse.c', line 89

static VALUE
body_get(VALUE self) {
    agooResponse	res = (agooResponse)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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/agoo/rresponse.c', line 112

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

    if (T_STRING == rb_type(val)) {
	if (NULL == (res->body = AGOO_STRDUP(StringValuePtr(val)))) {
	    rb_raise(rb_eArgError, "failed to copy 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.



134
135
136
137
# File 'ext/agoo/rresponse.c', line 134

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

#code=(val) ⇒ Object

call-seq: code=(value)

Sets the HTTP status code for the response.



145
146
147
148
149
150
151
152
153
154
155
# File 'ext/agoo/rresponse.c', line 145

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

    if (100 <= code && code < 600) {
	((agooResponse)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.



89
90
91
92
93
94
95
96
97
# File 'ext/agoo/rresponse.c', line 89

static VALUE
body_get(VALUE self) {
    agooResponse	res = (agooResponse)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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/agoo/rresponse.c', line 112

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

    if (T_STRING == rb_type(val)) {
	if (NULL == (res->body = AGOO_STRDUP(StringValuePtr(val)))) {
	    rb_raise(rb_eArgError, "failed to copy 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.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/agoo/rresponse.c', line 62

static VALUE
to_s(VALUE self) {
    agooResponse	res = (agooResponse)DATA_PTR(self);
    int			len = agoo_response_len(res);
    char		*s = (char*)AGOO_MALLOC(len + 1);

    if (NULL == s) {
	rb_raise(rb_eNoMemError, "out of memory");
    }
    agoo_response_fill(res, s);

    return rb_str_new(s, len);
}