summaryrefslogtreecommitdiffstats
path: root/nanohttp/nanohttp-stream.c
blob: 8eb31d68829a19c3388f48a92a0530242bfea2ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/******************************************************************
*  $Id: nanohttp-stream.c,v 1.1 2004/09/19 07:05:03 snowdrop Exp $
*
* CSOAP Project:  A http client/server library in C
* Copyright (C) 2003-2004  Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA  02111-1307, USA.
*
* Email: ferhatayaz@yahoo.com
******************************************************************/

#define STREAM_INVALID -1
#define STREAM_INVALID_TYPE -2
#define STREAM_SOCKET_ERROR -3
#define STREAM_NO_CHUNK_SIZE -4

#include <nanohttp/nanohttp-stream.h>

static
int _http_stream_is_content_length(hpair_t *header)
{
  return 
    hpairnode_get_ignore_case(header, HEADER_CONTENT_LENGTH) != NULL;
}

static
int _http_stream_is_chunked(hpair_t *header)
{
  char *chunked;
  chunked = hpairnode_get_ignore_case(header, HEADER_TRANSFER_ENCODING);
  if (chunked != NULL)
  {
      if (!strcmp(chunked, TRANSFER_ENCODING_CHUNKED))
      {
        return 1;
      }
  }

  return 0;
}

/**
  Creates a new input stream. 
*/
http_input_stream_t *http_input_stream_new(hsocket_t sock, hpair_t *header)
{
  http_input_stream_t *result;
  char                *content_length;
  char                *chunked;

  /* Paranoya check */
  if (header == NULL)
    return NULL;

  /* Create object */
  result = (http_input_stream_t*)malloc(sizeof(http_input_stream_t));
  result->sock = sock;

  /* Find connection type */
  
  /* Check if Content-type */
  if (_http_stream_is_content_length(header))
  {
    log_verbose1("Stream transfer with 'Content-length'");
    content_length = hpairnode_get_ignore_case(header, HEADER_CONTENT_LENGTH);
    result->content_length = atoi(content_length);
    result->received = 0;
    result->type =   HTTP_TRANSFER_CONTENT_LENGTH;
  }
  /* Check if Chunked */
  else if (_http_stream_is_chunked(header))
  {
    log_verbose1("Stream transfer with 'chunked'");
    result->type = HTTP_TRANSFER_CONTENT_CHUNKED;
    result->chunk_size = -1;
    result->received = -1;
  }
  /* Assume connection close */
  else
  {
    log_verbose1("Stream transfer with 'Connection: close'");
    result->type = HTTP_TRANSFER_CONNECTION_CLOSE;
  }

  return result;
}


/**
  Free input stream
*/
void http_input_stream_free(http_input_stream_t *stream)
{
  free(stream);
}

static 
int _http_input_stream_is_content_length_ready(
  http_input_stream_t *stream)
{
  return (stream->content_length > stream->received);
}

static 
int _http_input_stream_is_chunked_ready(
  http_input_stream_t *stream)
{
  return stream->chunk_size != 0;

}

static 
int _http_input_stream_is_connection_closed_ready(
  http_input_stream_t *stream)
{
/* TODO (#1#): implement */
}

static 
int _http_input_stream_content_length_read(
  http_input_stream_t *stream, byte_t *dest, size_t size)
{
  int status;

  /* check limit */
  if (stream->content_length - stream->received < size)
    size = stream->content_length - stream->received;

  /* read from socket */
  status = hsocket_read(stream->sock, dest, size, 1);
  if (status == -1)
    return STREAM_SOCKET_ERROR;

  stream->received += status;
  return status;
}

static 
int _http_input_stream_chunked_read_chunk_size(
  http_input_stream_t *stream)
{
  char  chunk[25];
  int chunk_size, status, i = 0;
  
  while (1)
  {
    status = hsocket_read(stream->sock, &(chunk[i]), 1, 1);

    if (status == -1)
        return STREAM_SOCKET_ERROR;

    if (chunk[i] == '\r' || chunk[i] == ';')
    {
        chunk[i] = '\0';
    }
    else if (chunk[i] == '\n')
    {
        chunk[i] = '\0'; /* double check*/
  			chunk_size = strtol(chunk, (char **) NULL, 16);	/* hex to dec */
  			log_debug3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size);
  			return chunk_size;
    }
    
    if (i == 24)
        return STREAM_NO_CHUNK_SIZE;
    else
        i++;
  }

  /* this should never happens */
  return STREAM_NO_CHUNK_SIZE; 
}

static 
int _http_input_stream_chunked_read(
  http_input_stream_t *stream, byte_t *dest, size_t size)
{
  int status;
  int remain, read=0;

  while (size > 0)
  {
    remain = stream->chunk_size - stream->received ;

    if (remain == 0)
    {
      /* receive new chunk size */
      stream->chunk_size = 
            _http_input_stream_chunked_read_chunk_size(stream);

      if (stream->chunk_size < 0)
      {
        /* TODO (#1#): set error flag */
        return stream->chunk_size;
      }
      else if (stream->chunk_size == 0)
      {
        return read;
      }
      remain = stream->chunk_size;
    }

    /* show remaining chunk size in socket */
    if (remain < size)
    {
      /* read from socket */
      status = hsocket_read(stream->sock, &(dest[read]), remain, 1);
      if (status == -1)
        return STREAM_SOCKET_ERROR;

    }
    else
    {
      /* read from socket */
      status = hsocket_read(stream->sock, &(dest[read]), size, 1);
      if (status == -1)
        return STREAM_SOCKET_ERROR;
    }

    read += status;
    size -= status;
    stream->received += status;
  }
}

static 
int _http_input_stream_connection_closed_read(
  http_input_stream_t *stream, byte_t *dest, size_t size)
{
/* TODO (#1#): implement */
  return 0;
}

/**
  Returns the actual status of the stream.
*/
int http_input_stream_is_ready(http_input_stream_t *stream)
{
  /* paranoya check */
  if (stream == NULL)
    return 0;

  switch (stream->type)
  {
    case HTTP_TRANSFER_CONTENT_LENGTH:
        return _http_input_stream_is_content_length_ready(stream);
    case HTTP_TRANSFER_CONTENT_CHUNKED:
        return _http_input_stream_is_chunked_ready(stream);
    case HTTP_TRANSFER_CONNECTION_CLOSE:
        return _http_input_stream_is_connection_closed_ready(stream);
    default:
        return 0;
  }
}

/**
  Returns the actual read bytes
  <0 on error
*/
int http_input_stream_read(http_input_stream_t *stream, 
                           byte_t *dest, size_t size)
{
  /* paranoya check */
  if (stream == NULL)
    return STREAM_INVALID;

  switch (stream->type)
  {
    case HTTP_TRANSFER_CONTENT_LENGTH:
        return _http_input_stream_content_length_read(stream, dest, size);
    case HTTP_TRANSFER_CONTENT_CHUNKED:
        return _http_input_stream_chunked_read(stream, dest, size);
    case HTTP_TRANSFER_CONNECTION_CLOSE:
        return _http_input_stream_connection_closed_read(stream, dest, size);
    default:
        return STREAM_INVALID_TYPE;
  }
}