taskrambler  0.1.9
Web server and task management solution.
http/writer/write.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 <unistd.h>
24 #include <sys/stat.h>
25 
26 #include "class.h"
27 #include "http/message.h"
28 #include "queue.h"
29 #include "http/writer.h"
30 #include "stream.h"
31 
32 #include "commons.h"
33 #include "utils/memory.h"
34 
35 
36 ssize_t
37 httpWriterWrite(void * _this, Stream st)
38 {
39  HttpWriter this = _this;
40 
41  switch (this->state) {
42  char * start;
43  ssize_t to_write;
44  ssize_t written;
45 
46  case HTTP_WRITER_GET:
47 
48  if (! queueEmpty(this->queue)) {
49  this->current = queueGet(this->queue);
50 
51  this->written = 0;
52  this->nheader = httpMessageHeaderSizeGet(this->current);
53 
54  if (this->nheader > memGetSize(this->buffer)) {
55  ssize_t size = this->nheader;
56 
57  size = (0 != size%WRITER_BUF_CHUNK)?
58  (size/WRITER_BUF_CHUNK)+1 :
59  size/WRITER_BUF_CHUNK;
60  size *= WRITER_BUF_CHUNK;
61 
62  if (NULL != this->buffer) {
63  MEM_FREE(this->buffer);
64  }
65 
66  this->buffer = memMalloc(size);
67  this->nbuffer = size;
68  }
69 
70  httpMessageHeaderToString(this->current, this->buffer);
71 
72  this->nbody = MIN(
73  this->current->nbody,
74  this->nbuffer - this->nheader);
75 
76  memcpy(
77  this->buffer + this->nheader,
78  this->current->body,
79  this->nbody);
80 
81  this->state = HTTP_WRITER_WRITE;
82  }
83  else {
84  break;
85  }
86 
87  case HTTP_WRITER_WRITE:
88 
89  if (this->written >= this->nbuffer) {
90  size_t body_done = this->written - this->nheader;
91 
92  start = this->current->body + body_done;
93  to_write = this->current->nbody - body_done;
94  } else {
95  start = this->buffer + this->written;
96  to_write = (this->nheader + this->nbody) - this->written;
97  }
98 
99  written = streamWrite(st, start, to_write);
100 
101  if (written < 0) {
102  return written;
103  }
104 
105  this->written += written;
106 
107  if (written != to_write) {
108  /*
109  * for some reason not all data could be
110  * written...most likely its a slow connection
111  * so, not to slow down the server we stop
112  * writing to this one now and come back to
113  * it in the next run....maybe it would be
114  * feasable to also implement some kind of
115  * timeout mechanism for writes...
116  * By the way, the same is true for reading,
117  * so to say, the parser.
118  */
119  break;
120  }
121 
122  if (this->written >= this->nheader + this->current->nbody) {
123  // we are done with this message.
124  this->state = HTTP_WRITER_DONE;
125  } else {
126  break;
127  }
128 
129  case HTTP_WRITER_DONE:
130 
131  this->state = HTTP_WRITER_GET;
132 
133  if (! httpMessageHasKeepAlive(this->current)) {
134  /**
135  * if the message did not have the keep-alive feature
136  * we don't care about further pipelined messages and
137  * return to the caller with a -2 indicating that the
138  * underlying connection should be closed at their side.
139  * Then we close to connection.
140  */
141  delete(this->current);
142  return -2;
143  }
144  delete(this->current);
145 
146  break;
147  }
148 
149  return NULL == this->current ?
150  this->queue->nmsg :
151  this->queue->nmsg + 1;
152 }
153 
154 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
#define MIN(a, b)
Definition: commons.h:35
void * queueGet(Queue)
Definition: queue/get.c:27
size_t memGetSize(void *)
Definition: memory.c:849
void * memMalloc(size_t)
Definition: memory.c:783
ssize_t streamWrite(Stream, void *, size_t)
Definition: stream/write.c:35
static size_t size
size_t httpMessageHeaderSizeGet(HttpMessage)
#define WRITER_BUF_CHUNK
Definition: http/writer.h:70
ssize_t httpWriterWrite(void *_this, Stream st)
char * httpMessageHeaderToString(HttpMessage, char *)
#define queueEmpty(this)
Definition: queue.h:55
char httpMessageHasKeepAlive(HttpMessage)