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.



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

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.



167
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 167

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;
	    }
	    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 (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)ALLOC_N(char, sizeof(struct _agooHeader) - 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.



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

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.



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

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

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



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

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.



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

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.



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

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.



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

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

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



46
47
48
49
50
51
52
53
54
55
56
# 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 = ALLOC_N(char, len + 1);

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