Server 0.0.1
HTTP/REST server implementation

src/http/response.c

Go to the documentation of this file.
00001 
00023 #include <stdlib.h>
00024 #include <stdarg.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 #include <stdio.h>
00028 
00029 #include "class.h"
00030 #include "utils/memory.h"
00031 
00032 #include "http/response.h"
00033 #include "http/interface/http_intro.h"
00034 
00035 
00036 static
00037 int
00038 httpResponseCtor(void * _this, va_list * params)
00039 {
00040         HttpResponse this = _this;
00041         char * reason;
00042 
00043         PARENTCALL(_this, Class, ctor, params);
00044 
00045         this->status = va_arg(* params, unsigned int);
00046         reason       = va_arg(* params, char *);
00047 
00048         this->reason  = calloc(1, strlen(reason)+1);
00049         strcpy(this->reason, reason);
00050 
00051         return 0;
00052 }
00053 
00054 static
00055 void
00056 httpResponseDtor(void * _this)
00057 {
00058         HttpResponse this = _this;
00059 
00060         FREE(this->reason);
00061 
00062         PARENTCALL(_this, Class, dtor);
00063 } 
00064 
00065 static
00066 size_t
00067 sizeGet(void * _this)
00068 {
00069         HttpResponse this = _this;
00070         size_t       size = 0;
00071 
00072         size += strlen(((HttpMessage)this)->version) + 1;
00073         size += 3 + 1;  // for status
00074         size += strlen(this->reason) + 2;
00075 
00076         return size;
00077 }
00078 
00079 static
00080 char *
00081 toString(void * _this, char * string)
00082 {
00083         HttpResponse this   = _this;
00084 
00085         strcpy(string, ((HttpMessage)this)->version);
00086         string   += strlen(string);
00087         *string++ = ' ';
00088 
00089         snprintf(string, 4, "%d", this->status);
00090         string   += strlen(string);
00091         *string++ = ' ';
00092 
00093         strcpy(string, this->reason);
00094         string   += strlen(string);
00095         *string++ = '\r';
00096         *string++ = '\n';
00097 
00098         return string;
00099 }
00100 
00101 INIT_IFACE(Class, httpResponseCtor, httpResponseDtor, NULL);
00102 INIT_IFACE(HttpIntro, sizeGet, toString);
00103 CREATE_CLASS(
00104                 HttpResponse,
00105                 HttpMessage,
00106                 IFACE(Class),
00107                 IFACE(HttpIntro));
00108 
00109 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines