http.c 19.8 KB
Newer Older
Youness Alaoui's avatar
Youness Alaoui committed
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
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2008 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2008 Nokia Corporation. All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Youness Alaoui, Collabora Ltd.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */

/*
 * Implementation of TCP relay socket interface using TCP Berkeley sockets. (See
 * http://en.wikipedia.org/wiki/Berkeley_sockets.)
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "http.h"
46
#include "agent-priv.h"
47
#include "socket-priv.h"
Youness Alaoui's avatar
Youness Alaoui committed
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

#include <string.h>
#include <stdlib.h>


#ifndef G_OS_WIN32
#include <unistd.h>
#endif


#define HTTP_USER_AGENT "libnice"

typedef enum {
  HTTP_STATE_INIT,
  HTTP_STATE_HEADERS,
  HTTP_STATE_BODY,
  HTTP_STATE_CONNECTED,
  HTTP_STATE_ERROR
} HttpState;

typedef struct {
  HttpState state;
  NiceSocket *base_socket;
  NiceAddress addr;
  gchar *username;
  gchar *password;
  GQueue send_queue;
75
76
77
78
79
80
81
82
83

  /* Ring buffer for receiving HTTP headers into before they’re parsed. */
  guint8 *recv_buf;
  gsize recv_buf_length;  /* allocation size of @recv_buf */
  gsize recv_buf_pos;  /* offset from @recv_buf of the 0th byte in the buffer */
  gsize recv_buf_fill;  /* number of bytes occupied in the buffer */

  /* Parsed from the Content-Length header provided by the other endpoint. */
  gsize content_length;
Youness Alaoui's avatar
Youness Alaoui committed
84
85
86
87
} HttpPriv;


static void socket_close (NiceSocket *sock);
88
89
static gint socket_recv_messages (NiceSocket *sock,
    NiceInputMessage *recv_messages, guint n_recv_messages);
90
static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
91
    const NiceOutputMessage *messages, guint n_messages);
92
93
static gint socket_send_messages_reliable (NiceSocket *sock,
    const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages);
Youness Alaoui's avatar
Youness Alaoui committed
94
static gboolean socket_is_reliable (NiceSocket *sock);
95
96
97
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
    NiceSocketWritableCb callback, gpointer user_data);
98
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
Youness Alaoui's avatar
Youness Alaoui committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

NiceSocket *
nice_http_socket_new (NiceSocket *base_socket,
    NiceAddress *addr, gchar *username, gchar *password)
{
  HttpPriv *priv;
  NiceSocket *sock = NULL;

  if (addr) {
    sock = g_slice_new0 (NiceSocket);
    sock->priv = priv = g_slice_new0 (HttpPriv);

    priv->base_socket = base_socket;
    priv->addr = *addr;
    priv->username = g_strdup (username);
    priv->password = g_strdup (password);
    priv->recv_buf = NULL;
116
117
118
    priv->recv_buf_length = 0;
    priv->recv_buf_pos = 0;
    priv->recv_buf_fill = 0;
Youness Alaoui's avatar
Youness Alaoui committed
119
120
    priv->content_length = 0;

121
    sock->type = NICE_SOCKET_TYPE_HTTP;
Youness Alaoui's avatar
Youness Alaoui committed
122
123
    sock->fileno = priv->base_socket->fileno;
    sock->addr = priv->base_socket->addr;
124
    sock->send_messages = socket_send_messages;
125
    sock->send_messages_reliable = socket_send_messages_reliable;
126
    sock->recv_messages = socket_recv_messages;
Youness Alaoui's avatar
Youness Alaoui committed
127
    sock->is_reliable = socket_is_reliable;
128
129
    sock->can_send = socket_can_send;
    sock->set_writable_callback = socket_set_writable_callback;
130
    sock->is_based_on = socket_is_based_on;
Youness Alaoui's avatar
Youness Alaoui committed
131
132
133
134
135
136
137
138
    sock->close = socket_close;

    /* Send HTTP CONNECT */
    {
      gchar *msg = NULL;
      gchar *credential = NULL;
      gchar host[INET6_ADDRSTRLEN];
      gint port = nice_address_get_port (&priv->addr);
139
140
141
      GOutputVector local_bufs;
      NiceOutputMessage local_messages;

Youness Alaoui's avatar
Youness Alaoui committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
      nice_address_to_string (&priv->addr, host);

      if (username) {
        gchar * userpass = g_strdup_printf ("%s:%s", username,
            password ? password : "");
        gchar * auth = g_base64_encode ((guchar *)userpass, strlen (userpass));
        credential = g_strdup_printf ("Proxy-Authorization: Basic %s\r\n", auth);
        g_free (auth);
        g_free (userpass);
      }
      msg = g_strdup_printf ("CONNECT %s:%d HTTP/1.0\r\n"
          "Host: %s\r\n"
          "User-Agent: %s\r\n"
          "Content-Length: 0\r\n"
          "Proxy-Connection: Keep-Alive\r\n"
          "Connection: Keep-Alive\r\n"
          "Cache-Control: no-cache\r\n"
          "Pragma: no-cache\r\n"
          "%s\r\n", host, port, host, HTTP_USER_AGENT,
          credential? credential : "" );
      g_free (credential);

164
165
166
167
168
      local_bufs.buffer = msg;
      local_bufs.size = strlen (msg);
      local_messages.buffers = &local_bufs;
      local_messages.n_buffers = 1;

169
170
      nice_socket_send_messages_reliable (priv->base_socket, NULL,
          &local_messages, 1);
Youness Alaoui's avatar
Youness Alaoui committed
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
      priv->state = HTTP_STATE_INIT;
      g_free (msg);
    }
  }

  return sock;
}


static void
socket_close (NiceSocket *sock)
{
  HttpPriv *priv = sock->priv;

  if (priv->base_socket)
    nice_socket_free (priv->base_socket);

  if (priv->username)
    g_free (priv->username);

  if (priv->password)
    g_free (priv->password);

  if (priv->recv_buf)
    g_free (priv->recv_buf);

197
  nice_socket_free_send_queue (&priv->send_queue);
Youness Alaoui's avatar
Youness Alaoui committed
198
199

  g_slice_free(HttpPriv, sock->priv);
200
  sock->priv = NULL;
Youness Alaoui's avatar
Youness Alaoui committed
201
202
}

203
204
205
static void
assert_ring_buffer_valid (HttpPriv *priv)
{
206
  g_assert_cmpint (priv->recv_buf_fill, <=, priv->recv_buf_length);
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
  g_assert (priv->recv_buf_pos == 0 ||
      priv->recv_buf_pos < priv->recv_buf_length);
  g_assert (priv->recv_buf_length == 0 || priv->recv_buf != NULL);
}

/* Pops up to @buffer_length bytes off the ring buffer and copies them into
 * @buffer. Returns the number of bytes copied. */
static gsize
memcpy_ring_buffer_to_buffer (HttpPriv *priv,
    guint8 *buffer, gsize buffer_length)
{
  gsize len, consumed = 0;
  gboolean has_wrapped;

  has_wrapped =
      (priv->recv_buf_pos + priv->recv_buf_fill) > priv->recv_buf_length;

  if (has_wrapped) {
    len = MIN (priv->recv_buf_length - priv->recv_buf_pos, buffer_length);
    memcpy (buffer, priv->recv_buf + priv->recv_buf_pos, len);
    consumed += len;

    buffer += len;
    buffer_length -= len;

    len = MIN (priv->recv_buf_fill - len, buffer_length);
    memcpy (buffer, priv->recv_buf, len);
    consumed += len;
  } else {
    len = MIN (priv->recv_buf_fill, buffer_length);
    memcpy (buffer, priv->recv_buf + priv->recv_buf_pos, len);
    consumed += len;
  }

  priv->recv_buf_pos =
      (priv->recv_buf_pos + consumed) % priv->recv_buf_length;
  priv->recv_buf_fill -= consumed;
Youness Alaoui's avatar
Youness Alaoui committed
244

245
246
247
248
249
250
251
  return consumed;
}

/* Returns the number of messages touched. Silently drops any data from @buffer
 * which doesn’t fit in @messages. Updates the ring buffer to pop the copied
 * data off it. Treats all #GInputVectors in @messages the same; there is no
 * differentiation between different #NiceInputMessages. */
Youness Alaoui's avatar
Youness Alaoui committed
252
static gint
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
memcpy_ring_buffer_to_input_messages (HttpPriv *priv,
    NiceInputMessage *messages, guint n_messages)
{
  guint i, j;

  for (i = 0; priv->recv_buf_fill > 0 && i < n_messages; i++) {
    NiceInputMessage *message = &messages[i];

    for (j = 0;
         priv->recv_buf_fill > 0 &&
         ((message->n_buffers >= 0 && j < (guint) message->n_buffers) ||
          (message->n_buffers < 0 && message->buffers[j].buffer != NULL));
         j++) {
      message->buffers[j].size =
          memcpy_ring_buffer_to_buffer (priv,
              message->buffers[j].buffer, message->buffers[j].size);
    }
  }

  return i;
}

/* FIXME: The current implementation of socket_recv_message() is a fast
 * pass-through to nice_socket_recv_message() if the HTTP socket is connected,
 * but is a slow state machine otherwise, using multiple memcpy()s. Spruce it up
 * to better to use the recv_messages to avoid the memcpy()s. */
static gint
socket_recv_messages (NiceSocket *sock,
    NiceInputMessage *recv_messages, guint n_recv_messages)
Youness Alaoui's avatar
Youness Alaoui committed
282
283
{
  HttpPriv *priv = sock->priv;
284
  gint ret = -1;
Youness Alaoui's avatar
Youness Alaoui committed
285

286
287
  /* Make sure socket has not been freed: */
  g_assert (sock->priv != NULL);
288

289
290
  if (priv->state == HTTP_STATE_CONNECTED) {
    guint i;
291

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    /* Fast path: pass through to the base socket once we’re connected. */
    if (priv->base_socket) {
      ret = nice_socket_recv_messages (priv->base_socket,
          recv_messages, n_recv_messages);
    }

    if (ret <= 0)
      return ret;

    /* After successfully receiving into at least one NiceInputMessage,
     * update the from address in each valid NiceInputMessage. */
    for (i = 0; i < (guint) ret; i++) {
      if (recv_messages[i].from != NULL)
        *recv_messages[i].from = priv->addr;
    }
Youness Alaoui's avatar
Youness Alaoui committed
307

308
    return ret;
Youness Alaoui's avatar
Youness Alaoui committed
309
  } else {
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
    /* Slow path: read into a local ring buffer until we’re parsed enough of the
     * headers. Double the buffer in size every time it fills up. */
    gboolean has_wrapped;
    GInputVector local_recv_bufs[2];
    NiceInputMessage local_recv_message = { local_recv_bufs, 2, NULL, 0 };

    /* Has the buffer filled up? Start with an initial buffer of 1KB, which
     * should cover the average size of HTTP response headers. Source:
     * http://dev.chromium.org/spdy/spdy-whitepaper */
    if (priv->recv_buf_fill == priv->recv_buf_length) {
      priv->recv_buf_length = MAX (priv->recv_buf_length * 2, 1024);
      priv->recv_buf = g_realloc (priv->recv_buf, priv->recv_buf_length);
    }

    assert_ring_buffer_valid (priv);

    /* Read some data into the buffer. Use two GInputVectors: one for the tail
     * of the buffer and one for the head. */
    has_wrapped =
        (priv->recv_buf_pos + priv->recv_buf_fill) > priv->recv_buf_length;

    if (has_wrapped) {
      local_recv_bufs[0].buffer =
           priv->recv_buf + (priv->recv_buf_pos + priv->recv_buf_fill) %
           priv->recv_buf_length;
      local_recv_bufs[0].size = priv->recv_buf_length - priv->recv_buf_fill;
      local_recv_bufs[1].buffer = NULL;
      local_recv_bufs[1].size = 0;
    } else {
      local_recv_bufs[0].buffer =
          priv->recv_buf + priv->recv_buf_pos + priv->recv_buf_fill;
      local_recv_bufs[0].size =
          priv->recv_buf_length - (priv->recv_buf_pos + priv->recv_buf_fill);
      local_recv_bufs[1].buffer = priv->recv_buf;
      local_recv_bufs[1].size = priv->recv_buf_pos;
    }

    if (priv->base_socket) {
      ret = nice_socket_recv_messages (priv->base_socket,
          &local_recv_message, 1);
    }

    if (ret <= 0)
      return ret;

    /* Update the buffer’s metadata. */
    priv->recv_buf_fill += local_recv_message.length;
    assert_ring_buffer_valid (priv);

    /* Fall through and try parsing the newly received data. */
Youness Alaoui's avatar
Youness Alaoui committed
360
361
  }

362
363
364
365
366
367
368
369
370
371
372
373
374
#define GET_BYTE(pos) \
  priv->recv_buf[(pos + priv->recv_buf_pos) % priv->recv_buf_length]
#define EAT_WHITESPACE(pos) \
  while (pos < priv->recv_buf_fill && GET_BYTE(pos) == ' ') \
    pos++; \
  if (pos >= priv->recv_buf_fill) \
    goto not_enough_data;

retry:
  nice_debug ("Receiving from HTTP proxy (state %d) : %" G_GSSIZE_FORMAT " \n"
      "'%s'", priv->state, priv->recv_buf_fill,
      priv->recv_buf + priv->recv_buf_pos);

Youness Alaoui's avatar
Youness Alaoui committed
375
376
377
  switch (priv->state) {
    case HTTP_STATE_INIT:
      {
378
379
380
381
        /* This is a logical position in the recv_buf; add
         * (priv->recv_buf + priv->recv_buf_pos) to get the actual byte in
         * memory. */
        guint pos = 0;
Youness Alaoui's avatar
Youness Alaoui committed
382

383
384
        /* Eat leading whitespace and check we have enough data. */
        EAT_WHITESPACE (pos);
Youness Alaoui's avatar
Youness Alaoui committed
385

386
        if (pos + 7 > priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
387
          goto not_enough_data;
388
389
390
391
392
393
394
        if (GET_BYTE (pos + 0) != 'H' ||
            GET_BYTE (pos + 1) != 'T' ||
            GET_BYTE (pos + 2) != 'T' ||
            GET_BYTE (pos + 3) != 'P' ||
            GET_BYTE (pos + 4) != '/' ||
            GET_BYTE (pos + 5) != '1' ||
            GET_BYTE (pos + 6) != '.')
Youness Alaoui's avatar
Youness Alaoui committed
395
396
397
          goto error;
        pos += 7;

398
        if (pos >= priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
399
          goto not_enough_data;
400
        if (GET_BYTE (pos) != '0' && GET_BYTE (pos) != '1')
Youness Alaoui's avatar
Youness Alaoui committed
401
402
403
404
          goto error;
        pos++;

        /* Make sure we have a space after the HTTP version */
405
        if (pos >= priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
406
          goto not_enough_data;
407
        if (GET_BYTE (pos) != ' ')
Youness Alaoui's avatar
Youness Alaoui committed
408
409
          goto error;

410
        EAT_WHITESPACE (pos);
Youness Alaoui's avatar
Youness Alaoui committed
411

412
413
        /* Check for a successful 2xx code */
        if (pos + 3 > priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
414
          goto not_enough_data;
415
416
417
        if (GET_BYTE (pos) != '2' ||
            GET_BYTE (pos + 1) < '0' || GET_BYTE (pos + 1) > '9' ||
            GET_BYTE (pos + 2) < '0' || GET_BYTE (pos + 2) > '9')
Youness Alaoui's avatar
Youness Alaoui committed
418
419
420
          goto error;

        /* Clear any trailing chars */
421
422
        while (pos + 1 < priv->recv_buf_fill &&
            GET_BYTE (pos) != '\r' && GET_BYTE (pos + 1) != '\n')
Youness Alaoui's avatar
Youness Alaoui committed
423
          pos++;
424
        if (pos + 1 >= priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
425
426
427
          goto not_enough_data;
        pos += 2;

428
429
430
        /* Consume the data we just parsed. */
        priv->recv_buf_pos = (priv->recv_buf_pos + pos) % priv->recv_buf_length;
        priv->recv_buf_fill -= pos;
Youness Alaoui's avatar
Youness Alaoui committed
431
432
433

        priv->content_length = 0;
        priv->state = HTTP_STATE_HEADERS;
434

Youness Alaoui's avatar
Youness Alaoui committed
435
436
437
438
439
        goto retry;
      }
      break;
    case HTTP_STATE_HEADERS:
      {
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
        guint pos = 0;

        if (pos + 15 < priv->recv_buf_fill &&
            (GET_BYTE (pos +  0) == 'C' || GET_BYTE (pos +  0) == 'c') &&
            (GET_BYTE (pos +  1) == 'o' || GET_BYTE (pos +  1) == 'O') &&
            (GET_BYTE (pos +  2) == 'n' || GET_BYTE (pos +  2) == 'N') &&
            (GET_BYTE (pos +  3) == 't' || GET_BYTE (pos +  3) == 'T') &&
            (GET_BYTE (pos +  4) == 'e' || GET_BYTE (pos +  4) == 'E') &&
            (GET_BYTE (pos +  5) == 'n' || GET_BYTE (pos +  5) == 'N') &&
            (GET_BYTE (pos +  6) == 't' || GET_BYTE (pos +  6) == 'T') &&
             GET_BYTE (pos +  7) == '-' &&
            (GET_BYTE (pos +  8) == 'L' || GET_BYTE (pos +  8) == 'l') &&
            (GET_BYTE (pos +  9) == 'e' || GET_BYTE (pos +  9) == 'E') &&
            (GET_BYTE (pos + 10) == 'n' || GET_BYTE (pos + 10) == 'N') &&
            (GET_BYTE (pos + 11) == 'g' || GET_BYTE (pos + 11) == 'G') &&
            (GET_BYTE (pos + 12) == 't' || GET_BYTE (pos + 12) == 'T') &&
            (GET_BYTE (pos + 13) == 'h' || GET_BYTE (pos + 13) == 'H') &&
             GET_BYTE (pos + 14) == ':') {
          /* Found a Content-Length header. Parse and store the value. Note that
           * the HTTP standard allows for arbitrarily-big content lengths. We
           * limit it to G_MAXSIZE for sanity’s sake.
           *
           * The code below is equivalent to strtoul(input, NULL, 10), but
           * operates on a ring buffer. */
          pos += 15;
          EAT_WHITESPACE (pos);
          priv->content_length = 0;

          while (TRUE) {
            guint8 byte = GET_BYTE (pos);
            gint val = g_ascii_digit_value (byte);

            if (byte == '\r') {
              /* Reached the end of the value; fall out to the code below which
               * will grab the \n. */
              break;
            } else if (val == -1) {
              priv->content_length = 0;
              goto error;
            }

            /* Check for overflow. Don’t flag it as an error; just fall through
             * to the code below which will skip to the \r\n. */
            if (priv->content_length > G_MAXSIZE / 10 ||
                priv->content_length * 10 > G_MAXSIZE - val) {
              priv->content_length = 0;
              break;
            }

            priv->content_length = (priv->content_length * 10) + val;

            if (pos + 1 > priv->recv_buf_fill)
              goto not_enough_data;
            pos++;
          }
Youness Alaoui's avatar
Youness Alaoui committed
495
        }
496
497
498
499

        /* Skip over the header. */
        while (pos + 1 < priv->recv_buf_fill &&
            GET_BYTE (pos) != '\r' && GET_BYTE (pos + 1) != '\n')
Youness Alaoui's avatar
Youness Alaoui committed
500
          pos++;
501
502
503
504
505

        nice_debug ("pos = %u, fill = %" G_GSSIZE_FORMAT,
            pos, priv->recv_buf_fill);

        if (pos + 1 >= priv->recv_buf_fill)
Youness Alaoui's avatar
Youness Alaoui committed
506
507
508
          goto not_enough_data;
        pos += 2;

509
510
511
        /* Consume the data we just parsed. */
        priv->recv_buf_pos = (priv->recv_buf_pos + pos) % priv->recv_buf_length;
        priv->recv_buf_fill -= pos;
Youness Alaoui's avatar
Youness Alaoui committed
512
513
514

        if (pos == 2)
          priv->state = HTTP_STATE_BODY;
515

Youness Alaoui's avatar
Youness Alaoui committed
516
517
518
519
520
        goto retry;
      }
      break;
    case HTTP_STATE_BODY:
      {
521
522
        gsize consumed;

Youness Alaoui's avatar
Youness Alaoui committed
523
524
525
526
        if (priv->content_length == 0) {
          priv->state = HTTP_STATE_CONNECTED;
          goto retry;
        }
527
528

        if (priv->recv_buf_fill == 0)
Youness Alaoui's avatar
Youness Alaoui committed
529
530
          goto not_enough_data;

531
        consumed = MIN (priv->content_length, priv->recv_buf_fill);
Youness Alaoui's avatar
Youness Alaoui committed
532

533
534
535
        priv->recv_buf_pos =
            (priv->recv_buf_pos + consumed) % priv->recv_buf_length;
        priv->recv_buf_fill -= consumed;
Youness Alaoui's avatar
Youness Alaoui committed
536
        priv->content_length -= consumed;
537

Youness Alaoui's avatar
Youness Alaoui committed
538
539
540
541
542
        goto retry;
      }
      break;
    case HTTP_STATE_CONNECTED:
      {
543
        gsize len;
Youness Alaoui's avatar
Youness Alaoui committed
544

545
546
        len = memcpy_ring_buffer_to_input_messages (priv,
            recv_messages, n_recv_messages);
Youness Alaoui's avatar
Youness Alaoui committed
547
548

        /* Send the pending data */
549
550
        nice_socket_flush_send_queue (priv->base_socket,
            &priv->send_queue);
Youness Alaoui's avatar
Youness Alaoui committed
551

552
        return len;
Youness Alaoui's avatar
Youness Alaoui committed
553
554
      }
      break;
555
    case HTTP_STATE_ERROR:
Youness Alaoui's avatar
Youness Alaoui committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    default:
      /* Unknown status */
      goto error;
  }

 not_enough_data:
  return 0;

 error:
  nice_debug ("http error");
  if (priv->base_socket)
    nice_socket_free (priv->base_socket);
  priv->base_socket = NULL;
  priv->state = HTTP_STATE_ERROR;

  return -1;
}

574
static gint
575
576
socket_send_messages (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *messages, guint n_messages)
Youness Alaoui's avatar
Youness Alaoui committed
577
578
579
{
  HttpPriv *priv = sock->priv;

580
581
  /* Make sure socket has not been freed: */
  g_assert (sock->priv != NULL);
582

Youness Alaoui's avatar
Youness Alaoui committed
583
  if (priv->state == HTTP_STATE_CONNECTED) {
584
585
586
587
    /* Fast path. */
    if (!priv->base_socket)
      return -1;

588
589
    return nice_socket_send_messages (priv->base_socket, to, messages,
        n_messages);
Youness Alaoui's avatar
Youness Alaoui committed
590
  } else if (priv->state == HTTP_STATE_ERROR) {
591
    return -1;
Youness Alaoui's avatar
Youness Alaoui committed
592
  } else {
593
    return 0;
Youness Alaoui's avatar
Youness Alaoui committed
594
  }
595
596

  return n_messages;
Youness Alaoui's avatar
Youness Alaoui committed
597
598
}

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
static gint
socket_send_messages_reliable (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *messages, guint n_messages)
{
  HttpPriv *priv = sock->priv;

  if (priv->state == HTTP_STATE_CONNECTED) {
    /* Fast path. */
    if (!priv->base_socket)
      return -1;

    return nice_socket_send_messages_reliable (priv->base_socket, to, messages,
        n_messages);
  } else if (priv->state == HTTP_STATE_ERROR) {
    return -1;
  } else {
615
    nice_socket_queue_send (&priv->send_queue, to, messages, n_messages);
616
617
618
619
  }

  return n_messages;
}
Youness Alaoui's avatar
Youness Alaoui committed
620
621
622
623

static gboolean
socket_is_reliable (NiceSocket *sock)
{
624
625
626
  HttpPriv *priv = sock->priv;

  return nice_socket_is_reliable (priv->base_socket);
Youness Alaoui's avatar
Youness Alaoui committed
627
}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

static gboolean
socket_can_send (NiceSocket *sock, NiceAddress *addr)
{
  HttpPriv *priv = sock->priv;

  return nice_socket_can_send (priv->base_socket, addr);
}

static void
socket_set_writable_callback (NiceSocket *sock,
    NiceSocketWritableCb callback, gpointer user_data)
{
  HttpPriv *priv = sock->priv;

  nice_socket_set_writable_callback (priv->base_socket, callback, user_data);
}
645
646

static gboolean
647
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
648
{
649
  HttpPriv *priv = sock->priv;
650

651
652
  return (sock == other) ||
      (priv && nice_socket_is_based_on (priv->base_socket, other));
653
}