taskrambler  v0.1.8
Web server and task management solution.
http/writer.h File Reference
#include <sys/types.h>
#include "class.h"
#include "http/message.h"
#include "queue.h"
#include "stream.h"
#include "commons.h"
+ Include dependency graph for http/writer.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define WRITER_BUF_CHUNK   1024 * 10
 

Enumerations

enum  HttpWriterState { HTTP_WRITER_GET =0, HTTP_WRITER_WRITE, HTTP_WRITER_DONE }
 

Functions

 CLASS (HttpWriter)
 
ssize_t httpWriterWrite (void *, Stream)
 

Detailed Description

Write ready responses to an output stream.

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file http/writer.h.

Macro Definition Documentation

#define WRITER_BUF_CHUNK   1024 * 10

Definition at line 70 of file http/writer.h.

Referenced by httpWriterWrite().

Enumeration Type Documentation

Enumerator
HTTP_WRITER_GET 
HTTP_WRITER_WRITE 
HTTP_WRITER_DONE 

Definition at line 75 of file http/writer.h.

Function Documentation

CLASS ( HttpWriter  )

Definition at line 81 of file http/writer.h.

81  {
82  char * buffer;
83 
84  Queue queue;
85  HttpMessage current;
86 
87  size_t nbuffer; // size of buffer
88  size_t nheader; // size headers in buf
89  size_t nbody; // sizeof body in buffer
90  size_t written; // already written bytes
91 
92  HttpWriterState state;
93 };
HttpWriterState
Definition: http/writer.h:75
ssize_t httpWriterWrite ( void *  ,
Stream   
)

if the message did not have the keep-alive feature we don't care about further pipelined messages and return to the caller with a -2 indicating that the underlying connection should be closed at their side. Then we close to connection.

Definition at line 37 of file http/writer/write.c.

References HTTP_WRITER_DONE, HTTP_WRITER_GET, HTTP_WRITER_WRITE, httpMessageHasKeepAlive(), httpMessageHeaderSizeGet(), httpMessageHeaderToString(), MEM_FREE, memGetSize(), memMalloc(), MIN, queueEmpty, queueGet(), size, streamWrite(), and WRITER_BUF_CHUNK.

Referenced by httpWorkerWrite().

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 }
#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
char * httpMessageHeaderToString(HttpMessage, char *)
#define queueEmpty(this)
Definition: queue.h:55
char httpMessageHasKeepAlive(HttpMessage)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: