示例#1
0
/**
 * web_socket_client_include_header:
 * @self: the client
 * @name: the header name
 * @value: the header value
 *
 * Add an HTTP header (eg: for authentication) to the
 * HTTP request.
 */
void
web_socket_client_include_header (WebSocketClient *self,
                                  const gchar *name,
                                  const gchar *value)
{
  g_return_if_fail (WEB_SOCKET_IS_CLIENT (self));
  g_return_if_fail (self->handshake_started == FALSE);

  if (!self->include_headers)
      self->include_headers = web_socket_util_new_headers ();
  g_hash_table_insert (self->include_headers,
                       g_strdup (name), g_strdup (value));
}
示例#2
0
GHashTable *
cockpit_web_server_new_table (void)
{
  return web_socket_util_new_headers ();
}
示例#3
0
/**
 * web_socket_util_parse_headers:
 * @data: (array length=length): the input data
 * @length: length of data
 * @headers: (out): location to place HTTP header hash table
 *
 * Parse HTTP headers.
 *
 * The number of bytes parsed will be returned if parsing succeeds, including
 * the new line at the end of the request line. A negative value will be
 * returned if parsing fails.
 *
 * If the HTTP request line was truncated (ie: not all of it was present
 * within @length) then zero will be returned.
 *
 * The @headers returned will be allocated using web_socket_util_new_headers(),
 * and should be freed by the caller using g_free().
 *
 * Return value: zero if truncated, negative if fails, or number of
 *               characters parsed
 */
gssize
web_socket_util_parse_headers (const gchar *data,
                               gsize length,
                               GHashTable **headers)
{
  GHashTable *parsed_headers;
  const gchar *line;
  const gchar *colon;
  gsize consumed = 0;
  gboolean end = FALSE;
  gsize line_len;
  gchar *name;
  gchar *value;

  parsed_headers = web_socket_util_new_headers ();

  while (!end)
    {
      line = memchr (data, '\n', length);

      /* No line ending: need more data */
      if (line == NULL)
        {
          consumed = 0;
          break;
        }

      line++;
      line_len = (line - data);

      /* An empty line, all done */
      if ((data[0] == '\r' && data[1] == '\n') || data[0] == '\n')
        {
          end = TRUE;
        }

      /* A header line */
      else
        {
          colon = memchr (data, ':', length);
          if (!colon || colon >= line)
            {
              g_message ("received invalid header line: %.*s", (gint)line_len, data);
              consumed = -1;
              break;
            }

          name = g_strndup (data, colon - data);
          g_strstrip (name);
          value = g_strndup (colon + 1, line - (colon + 1));
          g_strstrip (value);
          g_hash_table_insert (parsed_headers, name, value);
        }

      consumed += line_len;
      data += line_len;
      length -= line_len;
    }

  if (consumed > 0)
    {
      if (headers)
          *headers = g_hash_table_ref (parsed_headers);
    }

  g_hash_table_unref (parsed_headers);

  return consumed;
}