taskrambler  0.1.9
Web server and task management solution.
response.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 
29 #include "class.h"
30 #include "utils/memory.h"
31 
32 #include "http/response.h"
34 
35 
36 static
37 int
38 httpResponseCtor(void * _this, va_list * params)
39 {
40  HttpResponse this = _this;
41  char * reason;
42 
43  PARENTCALL(_this, Class, ctor, params);
44 
45  this->status = va_arg(* params, unsigned int);
46  reason = va_arg(* params, char *);
47 
48  this->reason = memCalloc(1, strlen(reason)+1);
49  strcpy(this->reason, reason);
50 
51  return 0;
52 }
53 
54 static
55 void
56 httpResponseDtor(void * _this)
57 {
58  HttpResponse this = _this;
59 
60  MEM_FREE(this->reason);
61 
62  PARENTCALL(_this, Class, dtor);
63 }
64 
65 static
66 size_t
67 sizeGet(void * _this)
68 {
69  HttpResponse this = _this;
70  size_t size = 0;
71 
72  size += strlen(((HttpMessage)this)->version) + 1;
73  size += 3 + 1; // for status
74  size += strlen(this->reason) + 2;
75 
76  return size;
77 }
78 
79 static
80 char *
81 toString(void * _this, char * string)
82 {
83  HttpResponse this = _this;
84 
85  strcpy(string, ((HttpMessage)this)->version);
86  string += strlen(string);
87  *string++ = ' ';
88 
89  snprintf(string, 4, "%d", this->status);
90  string += strlen(string);
91  *string++ = ' ';
92 
93  strcpy(string, this->reason);
94  string += strlen(string);
95  *string++ = '\r';
96  *string++ = '\n';
97 
98  return string;
99 }
100 
102 INIT_IFACE(HttpIntro, sizeGet, toString);
104  HttpResponse,
105  HttpMessage,
106  IFACE(Class),
107  IFACE(HttpIntro));
108 
109 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static int httpResponseCtor(void *_this, va_list *params)
Definition: response.c:38
#define IFACE(name)
Definition: interface.h:34
static size_t size
static size_t sizeGet(void *_this)
Definition: response.c:67
void * memCalloc(size_t, size_t)
Definition: memory.c:818
static char * string
static char * toString(void *_this, char *string)
Definition: response.c:81
INIT_IFACE(Class, httpResponseCtor, httpResponseDtor, NULL)
CREATE_CLASS(HttpResponse, HttpMessage, IFACE(Class), IFACE(HttpIntro))
#define PARENTCALL(object, _iface, method,...)
Definition: class/class.h:120
static void httpResponseDtor(void *_this)
Definition: response.c:56