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.


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/agoo/rresponse.c', line 147

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.


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
# File 'ext/agoo/rresponse.c', line 168

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.


73
74
75
76
77
78
79
80
81
# File 'ext/agoo/rresponse.c', line 73

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.


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/agoo/rresponse.c', line 96

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.


118
119
120
121
# File 'ext/agoo/rresponse.c', line 118

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.


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

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.


73
74
75
76
77
78
79
80
81
# File 'ext/agoo/rresponse.c', line 73

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.


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/agoo/rresponse.c', line 96

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.


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'ext/agoo/rresponse.c', line 46

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);
}