1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-socket.c: Socket networking code.
5 * Copyright (C) 2000-2003, Ximian, Inc.
18 #include "soup-address.h"
19 #include "soup-socket.h"
20 #include "soup-marshal.h"
21 #include "soup-misc.h"
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
29 #define PARENT_TYPE G_TYPE_OBJECT
30 static GObjectClass *parent_class;
41 static guint signals[LAST_SIGNAL] = { 0 };
55 struct SoupSocketPrivate {
57 SoupAddress *local_addr, *remote_addr;
58 GIOChannel *iochannel;
67 guint read_tag, write_tag, error_tag;
70 GMutex *iolock, *addrlock;
74 #define soup_sockaddr_max sockaddr_in6
76 #define soup_sockaddr_max sockaddr_in
79 static void set_property (GObject *object, guint prop_id,
80 const GValue *value, GParamSpec *pspec);
81 static void get_property (GObject *object, guint prop_id,
82 GValue *value, GParamSpec *pspec);
85 init (GObject *object)
87 SoupSocket *sock = SOUP_SOCKET (object);
89 sock->priv = g_new0 (SoupSocketPrivate, 1);
90 sock->priv->sockfd = -1;
91 sock->priv->non_blocking = sock->priv->nodelay = TRUE;
92 sock->priv->reuseaddr = TRUE;
93 sock->priv->addrlock = g_mutex_new ();
94 sock->priv->iolock = g_mutex_new ();
98 disconnect_internal (SoupSocket *sock)
100 g_io_channel_unref (sock->priv->iochannel);
101 sock->priv->iochannel = NULL;
102 sock->priv->sockfd = -1;
104 if (sock->priv->read_tag) {
105 g_source_remove (sock->priv->read_tag);
106 sock->priv->read_tag = 0;
108 if (sock->priv->write_tag) {
109 g_source_remove (sock->priv->write_tag);
110 sock->priv->write_tag = 0;
112 if (sock->priv->error_tag) {
113 g_source_remove (sock->priv->error_tag);
114 sock->priv->error_tag = 0;
119 finalize (GObject *object)
121 SoupSocket *sock = SOUP_SOCKET (object);
123 if (sock->priv->iochannel)
124 disconnect_internal (sock);
126 if (sock->priv->local_addr)
127 g_object_unref (sock->priv->local_addr);
128 if (sock->priv->remote_addr)
129 g_object_unref (sock->priv->remote_addr);
131 if (sock->priv->watch)
132 g_source_remove (sock->priv->watch);
134 g_mutex_free (sock->priv->addrlock);
135 g_mutex_free (sock->priv->iolock);
139 G_OBJECT_CLASS (parent_class)->finalize (object);
143 class_init (GObjectClass *object_class)
145 parent_class = g_type_class_ref (PARENT_TYPE);
147 /* virtual method override */
148 object_class->finalize = finalize;
149 object_class->set_property = set_property;
150 object_class->get_property = get_property;
153 signals[CONNECT_RESULT] =
154 g_signal_new ("connect_result",
155 G_OBJECT_CLASS_TYPE (object_class),
157 G_STRUCT_OFFSET (SoupSocketClass, connect_result),
159 soup_marshal_NONE__INT,
163 g_signal_new ("readable",
164 G_OBJECT_CLASS_TYPE (object_class),
166 G_STRUCT_OFFSET (SoupSocketClass, readable),
168 soup_marshal_NONE__NONE,
171 g_signal_new ("writable",
172 G_OBJECT_CLASS_TYPE (object_class),
174 G_STRUCT_OFFSET (SoupSocketClass, writable),
176 soup_marshal_NONE__NONE,
178 signals[DISCONNECTED] =
179 g_signal_new ("disconnected",
180 G_OBJECT_CLASS_TYPE (object_class),
182 G_STRUCT_OFFSET (SoupSocketClass, disconnected),
184 soup_marshal_NONE__NONE,
186 signals[NEW_CONNECTION] =
187 g_signal_new ("new_connection",
188 G_OBJECT_CLASS_TYPE (object_class),
190 G_STRUCT_OFFSET (SoupSocketClass, new_connection),
192 soup_marshal_NONE__OBJECT,
197 g_object_class_install_property (
198 object_class, PROP_NON_BLOCKING,
199 g_param_spec_boolean (SOUP_SOCKET_FLAG_NONBLOCKING,
201 "Whether or not the socket uses non-blocking I/O",
204 g_object_class_install_property (
205 object_class, PROP_NODELAY,
206 g_param_spec_boolean (SOUP_SOCKET_FLAG_NODELAY,
208 "Whether or not the socket uses TCP NODELAY",
211 g_object_class_install_property (
212 object_class, PROP_REUSEADDR,
213 g_param_spec_boolean (SOUP_SOCKET_FLAG_REUSEADDR,
215 "Whether or not the socket uses the TCP REUSEADDR flag",
218 g_object_class_install_property (
219 object_class, PROP_IS_SERVER,
220 g_param_spec_boolean (SOUP_SOCKET_IS_SERVER,
222 "Whether or not the socket is a server socket",
225 g_object_class_install_property (
226 object_class, PROP_SSL_CREDENTIALS,
227 g_param_spec_pointer (SOUP_SOCKET_SSL_CREDENTIALS,
229 "SSL credential information, passed from the session to the SSL implementation",
233 SOUP_MAKE_TYPE (soup_socket, SoupSocket, class_init, init, PARENT_TYPE)
237 update_fdflags (SoupSocket *sock)
241 if (sock->priv->sockfd == -1)
244 flags = fcntl (sock->priv->sockfd, F_GETFL, 0);
246 if (sock->priv->non_blocking)
249 flags &= ~O_NONBLOCK;
250 fcntl (sock->priv->sockfd, F_SETFL, flags);
253 opt = (sock->priv->nodelay != 0);
254 setsockopt (sock->priv->sockfd, IPPROTO_TCP,
255 TCP_NODELAY, &opt, sizeof (opt));
257 opt = (sock->priv->reuseaddr != 0);
258 setsockopt (sock->priv->sockfd, SOL_SOCKET,
259 SO_REUSEADDR, &opt, sizeof (opt));
263 set_property (GObject *object, guint prop_id,
264 const GValue *value, GParamSpec *pspec)
266 SoupSocket *sock = SOUP_SOCKET (object);
269 case PROP_NON_BLOCKING:
270 sock->priv->non_blocking = g_value_get_boolean (value);
271 update_fdflags (sock);
274 sock->priv->nodelay = g_value_get_boolean (value);
275 update_fdflags (sock);
278 sock->priv->reuseaddr = g_value_get_boolean (value);
279 update_fdflags (sock);
281 case PROP_SSL_CREDENTIALS:
282 sock->priv->ssl_creds = g_value_get_pointer (value);
290 get_property (GObject *object, guint prop_id,
291 GValue *value, GParamSpec *pspec)
293 SoupSocket *sock = SOUP_SOCKET (object);
296 case PROP_NON_BLOCKING:
297 g_value_set_boolean (value, sock->priv->non_blocking);
300 g_value_set_boolean (value, sock->priv->nodelay);
303 g_value_set_boolean (value, sock->priv->reuseaddr);
306 g_value_set_boolean (value, sock->priv->is_server);
308 case PROP_SSL_CREDENTIALS:
309 g_value_set_pointer (value, sock->priv->ssl_creds);
319 * @optname1: name of first property to set (or %NULL)
320 * @...: value of @optname1, followed by additional property/value pairs
322 * Return value: a new (disconnected) socket
325 soup_socket_new (const char *optname1, ...)
330 va_start (ap, optname1);
331 sock = (SoupSocket *)g_object_new_valist (SOUP_TYPE_SOCKET,
339 get_iochannel (SoupSocket *sock)
341 g_mutex_lock (sock->priv->iolock);
342 if (!sock->priv->iochannel) {
343 sock->priv->iochannel =
344 g_io_channel_unix_new (sock->priv->sockfd);
345 g_io_channel_set_close_on_unref (sock->priv->iochannel, TRUE);
346 g_io_channel_set_encoding (sock->priv->iochannel, NULL, NULL);
347 g_io_channel_set_buffered (sock->priv->iochannel, FALSE);
349 g_mutex_unlock (sock->priv->iolock);
350 return sock->priv->iochannel;
354 idle_connect_result (gpointer user_data)
356 SoupSocket *sock = user_data;
358 sock->priv->watch = 0;
360 g_signal_emit (sock, signals[CONNECT_RESULT], 0,
361 sock->priv->sockfd != -1 ? SOUP_STATUS_OK : SOUP_STATUS_CANT_CONNECT);
366 connect_watch (GIOChannel* iochannel, GIOCondition condition, gpointer data)
368 SoupSocket *sock = data;
370 int len = sizeof (error);
372 /* Remove the watch now in case we don't return immediately */
373 g_source_remove (sock->priv->watch);
374 sock->priv->watch = 0;
376 if (condition & ~(G_IO_IN | G_IO_OUT))
379 if (getsockopt (sock->priv->sockfd, SOL_SOCKET, SO_ERROR,
385 return idle_connect_result (sock);
388 g_signal_emit (sock, signals[CONNECT_RESULT], 0, SOUP_STATUS_CANT_CONNECT);
393 got_address (SoupAddress *addr, guint status, gpointer user_data)
395 SoupSocket *sock = user_data;
397 if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
398 g_signal_emit (sock, signals[CONNECT_RESULT], 0, status);
399 g_object_unref (sock);
403 soup_socket_connect (sock, sock->priv->remote_addr);
404 /* soup_socket_connect re-reffed addr */
405 g_object_unref (addr);
407 g_object_unref (sock);
411 * soup_socket_connect:
412 * @sock: a client #SoupSocket (which must not already be connected)
413 * @remote_addr: address to connect to
415 * If %SOUP_SOCKET_FLAG_NONBLOCKING has been set on the socket, this
416 * begins asynchronously connecting to the given address. The socket
417 * will emit %connect_result when it succeeds or fails (but not before
418 * returning from this function).
420 * If %SOUP_SOCKET_FLAG_NONBLOCKING has not been set, this will
421 * attempt to synchronously connect.
423 * Return value: %SOUP_STATUS_CONTINUE if connecting asynchronously,
424 * otherwise a success or failure code.
427 soup_socket_connect (SoupSocket *sock, SoupAddress *remote_addr)
432 g_return_val_if_fail (SOUP_IS_SOCKET (sock), SOUP_STATUS_MALFORMED);
433 g_return_val_if_fail (!sock->priv->is_server, SOUP_STATUS_MALFORMED);
434 g_return_val_if_fail (sock->priv->sockfd == -1, SOUP_STATUS_MALFORMED);
435 g_return_val_if_fail (SOUP_IS_ADDRESS (remote_addr), SOUP_STATUS_MALFORMED);
437 sock->priv->remote_addr = g_object_ref (remote_addr);
438 if (!sock->priv->non_blocking) {
439 status = soup_address_resolve_sync (remote_addr);
440 if (!SOUP_STATUS_IS_SUCCESSFUL (status))
444 sa = soup_address_get_sockaddr (sock->priv->remote_addr, &len);
446 if (!sock->priv->non_blocking)
447 return SOUP_STATUS_CANT_RESOLVE;
450 soup_address_resolve_async (remote_addr, got_address, sock);
451 return SOUP_STATUS_CONTINUE;
454 sock->priv->sockfd = socket (sa->sa_family, SOCK_STREAM, 0);
455 if (sock->priv->sockfd == -1) {
459 update_fdflags (sock);
461 status = connect (sock->priv->sockfd, sa, len);
465 if (errno == EINPROGRESS) {
466 /* Wait for connect to succeed or fail */
468 g_io_add_watch (get_iochannel (sock),
470 G_IO_PRI | G_IO_ERR |
471 G_IO_HUP | G_IO_NVAL,
472 connect_watch, sock);
473 return SOUP_STATUS_CONTINUE;
475 close (sock->priv->sockfd);
476 sock->priv->sockfd = -1;
481 if (sock->priv->non_blocking) {
482 sock->priv->watch = g_idle_add (idle_connect_result, sock);
483 return SOUP_STATUS_CONTINUE;
484 } else if (sock->priv->sockfd == -1)
485 return SOUP_STATUS_CANT_CONNECT;
487 get_iochannel (sock);
488 return SOUP_STATUS_OK;
493 listen_watch (GIOChannel* iochannel, GIOCondition condition, gpointer data)
495 SoupSocket *sock = data, *new;
496 struct soup_sockaddr_max sa;
499 if (condition & (G_IO_HUP | G_IO_ERR)) {
500 g_source_remove (sock->priv->watch);
501 sock->priv->watch = 0;
505 sa_len = sizeof (sa);
506 sockfd = accept (sock->priv->sockfd, (struct sockaddr *)&sa, &sa_len);
510 new = g_object_new (SOUP_TYPE_SOCKET, NULL);
511 new->priv->sockfd = sockfd;
512 new->priv->non_blocking = sock->priv->non_blocking;
513 new->priv->nodelay = sock->priv->nodelay;
514 new->priv->is_server = TRUE;
515 new->priv->ssl_creds = sock->priv->ssl_creds;
516 update_fdflags (new);
518 new->priv->remote_addr = soup_address_new_from_sockaddr ((struct sockaddr *)&sa, sa_len);
520 if (new->priv->ssl_creds) {
521 if (!soup_socket_start_ssl (new)) {
522 g_object_unref (new);
528 g_signal_emit (sock, signals[NEW_CONNECTION], 0, new);
529 g_object_unref (new);
535 * soup_socket_listen:
536 * @sock: a server #SoupSocket (which must not already be connected or
538 * @local_addr: Local address to bind to.
540 * Makes @sock start listening on the given interface and port. When
541 * connections come in, @sock will emit %new_connection.
543 * Return value: whether or not @sock is now listening.
546 soup_socket_listen (SoupSocket *sock, SoupAddress *local_addr)
551 g_return_val_if_fail (SOUP_IS_SOCKET (sock), FALSE);
552 g_return_val_if_fail (sock->priv->is_server, FALSE);
553 g_return_val_if_fail (sock->priv->sockfd == -1, FALSE);
554 g_return_val_if_fail (SOUP_IS_ADDRESS (local_addr), FALSE);
556 /* @local_addr may have its port set to 0. So we intentionally
557 * don't store it in sock->priv->local_addr, so that if the
558 * caller calls soup_socket_get_local_address() later, we'll
559 * have to make a new addr by calling getsockname(), which
560 * will have the right port number.
562 sa = soup_address_get_sockaddr (local_addr, &sa_len);
563 g_return_val_if_fail (sa != NULL, FALSE);
565 sock->priv->sockfd = socket (sa->sa_family, SOCK_STREAM, 0);
566 if (sock->priv->sockfd < 0)
568 update_fdflags (sock);
571 if (bind (sock->priv->sockfd, sa, sa_len) != 0)
575 if (listen (sock->priv->sockfd, 10) != 0)
578 sock->priv->watch = g_io_add_watch (get_iochannel (sock),
579 G_IO_IN | G_IO_ERR | G_IO_HUP,
584 if (sock->priv->sockfd != -1) {
585 close (sock->priv->sockfd);
586 sock->priv->sockfd = -1;
594 * soup_socket_start_ssl:
597 * Starts using SSL on @socket.
599 * Return value: success or failure
602 soup_socket_start_ssl (SoupSocket *sock)
604 GIOChannel *ssl_chan;
606 get_iochannel (sock);
607 ssl_chan = soup_ssl_wrap_iochannel (
608 sock->priv->iochannel, sock->priv->is_server ?
609 SOUP_SSL_TYPE_SERVER : SOUP_SSL_TYPE_CLIENT,
610 soup_address_get_name (sock->priv->remote_addr),
611 sock->priv->ssl_creds);
616 sock->priv->iochannel = ssl_chan;
622 * soup_socket_client_new_async:
623 * @hostname: remote machine to connect to
624 * @port: remote port to connect to
625 * @ssl_creds: SSL credentials structure, or %NULL if not SSL
626 * @callback: callback to call when the socket is connected
627 * @user_data: data for @callback
629 * Creates a connection to @hostname and @port. @callback will be
630 * called when the connection completes (or fails).
632 * Return value: the new socket (not yet ready for use).
635 soup_socket_client_new_async (const char *hostname, guint port,
637 SoupSocketCallback callback, gpointer user_data)
641 g_return_val_if_fail (hostname != NULL, NULL);
643 sock = g_object_new (SOUP_TYPE_SOCKET,
644 SOUP_SOCKET_SSL_CREDENTIALS, ssl_creds,
646 soup_socket_connect (sock, soup_address_new (hostname, port));
649 soup_signal_connect_once (sock, "connect_result",
650 G_CALLBACK (callback), user_data);
656 * soup_socket_client_new_sync:
657 * @hostname: remote machine to connect to
658 * @port: remote port to connect to
659 * @ssl_creds: SSL credentials structure, or %NULL if not SSL
660 * @status_ret: pointer to return the soup status in
662 * Creates a connection to @hostname and @port. If @status_ret is not
663 * %NULL, it will contain a status code on return.
665 * Return value: the new socket, or %NULL if it could not connect.
668 soup_socket_client_new_sync (const char *hostname, guint port,
669 gpointer ssl_creds, guint *status_ret)
674 g_return_val_if_fail (hostname != NULL, NULL);
676 sock = g_object_new (SOUP_TYPE_SOCKET,
677 SOUP_SOCKET_SSL_CREDENTIALS, ssl_creds,
679 sock->priv->non_blocking = FALSE;
680 status = soup_socket_connect (sock, soup_address_new (hostname, port));
682 if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
683 g_object_unref (sock);
688 *status_ret = status;
693 * soup_socket_server_new:
694 * @local_addr: Local address to bind to. (Use soup_address_any_new() to
695 * accept connections on any local address)
696 * @ssl_creds: SSL credentials, or %NULL if this is not an SSL server
697 * @callback: Callback to call when a client connects
698 * @user_data: data to pass to @callback.
700 * Create and open a new #SoupSocket listening on the specified
701 * address. @callback will be called each time a client connects,
702 * with a new #SoupSocket.
704 * Returns: a new #SoupSocket, or NULL if there was a failure.
707 soup_socket_server_new (SoupAddress *local_addr, gpointer ssl_creds,
708 SoupSocketListenerCallback callback,
713 g_return_val_if_fail (SOUP_IS_ADDRESS (local_addr), NULL);
715 sock = g_object_new (SOUP_TYPE_SOCKET,
716 SOUP_SOCKET_SSL_CREDENTIALS, ssl_creds,
718 sock->priv->is_server = TRUE;
719 if (!soup_socket_listen (sock, local_addr)) {
720 g_object_unref (sock);
725 g_signal_connect (sock, "new_connection",
726 G_CALLBACK (callback), user_data);
734 soup_socket_disconnect (SoupSocket *sock)
736 gboolean already_disconnected = FALSE;
738 g_return_if_fail (SOUP_IS_SOCKET (sock));
740 if (g_mutex_trylock (sock->priv->iolock)) {
741 if (sock->priv->iochannel)
742 disconnect_internal (sock);
744 already_disconnected = TRUE;
745 g_mutex_unlock (sock->priv->iolock);
749 /* Another thread is currently doing IO, so
750 * we can't close the iochannel. So just kick
751 * the file descriptor out from under it.
754 sockfd = sock->priv->sockfd;
755 sock->priv->sockfd = -1;
757 already_disconnected = TRUE;
759 g_io_channel_set_close_on_unref (sock->priv->iochannel,
765 if (already_disconnected)
768 /* Give all readers a chance to notice the connection close */
769 g_signal_emit (sock, signals[READABLE], 0);
771 /* FIXME: can't disconnect until all data is read */
773 /* Then let everyone know we're disconnected */
774 g_signal_emit (sock, signals[DISCONNECTED], 0);
778 soup_socket_is_connected (SoupSocket *sock)
780 g_return_val_if_fail (SOUP_IS_SOCKET (sock), FALSE);
782 return sock->priv->iochannel != NULL;
787 soup_socket_get_local_address (SoupSocket *sock)
789 g_return_val_if_fail (SOUP_IS_SOCKET (sock), NULL);
791 g_mutex_lock (sock->priv->addrlock);
792 if (!sock->priv->local_addr) {
793 struct soup_sockaddr_max bound_sa;
796 sa_len = sizeof (bound_sa);
797 getsockname (sock->priv->sockfd, (struct sockaddr *)&bound_sa, &sa_len);
798 sock->priv->local_addr = soup_address_new_from_sockaddr ((struct sockaddr *)&bound_sa, sa_len);
800 g_mutex_unlock (sock->priv->addrlock);
802 return sock->priv->local_addr;
806 soup_socket_get_remote_address (SoupSocket *sock)
808 g_return_val_if_fail (SOUP_IS_SOCKET (sock), NULL);
810 g_mutex_lock (sock->priv->addrlock);
811 if (!sock->priv->remote_addr) {
812 struct soup_sockaddr_max bound_sa;
815 sa_len = sizeof (bound_sa);
816 getpeername (sock->priv->sockfd, (struct sockaddr *)&bound_sa, &sa_len);
817 sock->priv->remote_addr = soup_address_new_from_sockaddr ((struct sockaddr *)&bound_sa, sa_len);
819 g_mutex_unlock (sock->priv->addrlock);
821 return sock->priv->remote_addr;
828 socket_read_watch (GIOChannel *chan, GIOCondition cond, gpointer user_data)
830 SoupSocket *sock = user_data;
832 sock->priv->read_tag = 0;
833 g_signal_emit (sock, signals[READABLE], 0);
838 static SoupSocketIOStatus
839 read_from_network (SoupSocket *sock, gpointer buffer, gsize len, gsize *nread)
843 if (!sock->priv->iochannel)
844 return SOUP_SOCKET_EOF;
846 status = g_io_channel_read_chars (sock->priv->iochannel,
847 buffer, len, nread, NULL);
849 case G_IO_STATUS_NORMAL:
850 case G_IO_STATUS_AGAIN:
852 return SOUP_SOCKET_OK;
854 if (!sock->priv->read_tag) {
855 sock->priv->read_tag =
856 g_io_add_watch (sock->priv->iochannel, G_IO_IN,
857 socket_read_watch, sock);
859 return SOUP_SOCKET_WOULD_BLOCK;
861 case G_IO_STATUS_EOF:
862 return SOUP_SOCKET_EOF;
865 return SOUP_SOCKET_ERROR;
869 static SoupSocketIOStatus
870 read_from_buf (SoupSocket *sock, gpointer buffer, gsize len, gsize *nread)
872 GByteArray *read_buf = sock->priv->read_buf;
874 *nread = MIN (read_buf->len, len);
875 memcpy (buffer, read_buf->data, *nread);
877 if (*nread == read_buf->len) {
878 g_byte_array_free (read_buf, TRUE);
879 sock->priv->read_buf = NULL;
881 memcpy (read_buf->data, read_buf->data + *nread,
882 read_buf->len - *nread);
883 g_byte_array_set_size (read_buf, read_buf->len - *nread);
886 return SOUP_SOCKET_OK;
892 * @buffer: buffer to read into
893 * @len: size of @buffer in bytes
894 * @nread: on return, the number of bytes read into @buffer
896 * Attempts to read up to @len bytes from @sock into @buffer. If some
897 * data is successfully read, soup_socket_read() will return
898 * %SOUP_SOCKET_OK, and *@nread will contain the number of bytes
901 * If @sock is non-blocking, and no data is available, the return
902 * value will be %SOUP_SOCKET_WOULD_BLOCK. In this case, the caller
903 * can connect to the %readable signal to know when there is more data
904 * to read. (NB: You MUST read all available data off the socket
905 * first. The %readable signal will only be emitted after
906 * soup_socket_read() has returned %SOUP_SOCKET_WOULD_BLOCK.)
908 * Return value: a #SoupSocketIOStatus, as described above (or
909 * %SOUP_SOCKET_EOF if the socket is no longer connected, or
910 * %SOUP_SOCKET_ERROR on any other error).
913 soup_socket_read (SoupSocket *sock, gpointer buffer, gsize len, gsize *nread)
915 SoupSocketIOStatus status;
917 g_return_val_if_fail (SOUP_IS_SOCKET (sock), SOUP_SOCKET_ERROR);
919 g_mutex_lock (sock->priv->iolock);
920 if (sock->priv->read_buf)
921 status = read_from_buf (sock, buffer, len, nread);
923 status = read_from_network (sock, buffer, len, nread);
924 g_mutex_unlock (sock->priv->iolock);
930 * soup_socket_read_until:
932 * @buffer: buffer to read into
933 * @len: size of @buffer in bytes
934 * @boundary: boundary to read until
935 * @boundary_len: length of @boundary in bytes
936 * @nread: on return, the number of bytes read into @buffer
937 * @got_boundary: on return, whether or not the data in @buffer
938 * ends with the boundary string
940 * Like soup_socket_read(), but reads no further than the first
941 * occurrence of @boundary. (If the boundary is found, it will be
942 * included in the returned data, and *@got_boundary will be set to
943 * %TRUE.) Any data after the boundary will returned in future reads.
945 * Return value: as for soup_socket_read()
948 soup_socket_read_until (SoupSocket *sock, gpointer buffer, gsize len,
949 gconstpointer boundary, gsize boundary_len,
950 gsize *nread, gboolean *got_boundary)
952 SoupSocketIOStatus status;
953 GByteArray *read_buf;
954 guint match_len, prev_len;
957 g_return_val_if_fail (SOUP_IS_SOCKET (sock), SOUP_SOCKET_ERROR);
958 g_return_val_if_fail (len >= boundary_len, SOUP_SOCKET_ERROR);
960 g_mutex_lock (sock->priv->iolock);
962 *got_boundary = FALSE;
964 if (!sock->priv->read_buf)
965 sock->priv->read_buf = g_byte_array_new ();
966 read_buf = sock->priv->read_buf;
968 if (read_buf->len < boundary_len) {
969 prev_len = read_buf->len;
970 g_byte_array_set_size (read_buf, len);
971 status = read_from_network (sock,
972 read_buf->data + prev_len,
973 len - prev_len, nread);
974 read_buf->len = prev_len + *nread;
976 if (status != SOUP_SOCKET_OK) {
977 g_mutex_unlock (sock->priv->iolock);
982 /* Scan for the boundary */
983 end = read_buf->data + read_buf->len;
984 for (p = read_buf->data; p <= end - boundary_len; p++) {
985 if (!memcmp (p, boundary, boundary_len)) {
987 *got_boundary = TRUE;
992 /* Return everything up to 'p' (which is either just after the
993 * boundary, or @boundary_len - 1 bytes before the end of the
996 match_len = p - read_buf->data;
997 status = read_from_buf (sock, buffer, MIN (len, match_len), nread);
999 g_mutex_unlock (sock->priv->iolock);
1004 socket_write_watch (GIOChannel *chan, GIOCondition condition, gpointer user_data)
1006 SoupSocket *sock = user_data;
1008 sock->priv->write_tag = 0;
1009 g_signal_emit (sock, signals[WRITABLE], 0);
1015 * soup_socket_write:
1017 * @buffer: data to write
1018 * @len: size of @buffer, in bytes
1019 * @nwrote: on return, number of bytes written
1021 * Attempts to write @len bytes from @buffer to @sock. If some data is
1022 * successfully written, the resturn status will be
1023 * %SOUP_SOCKET_SUCCESS, and *@nwrote will contain the number of bytes
1026 * If @sock is non-blocking, and no data could be written right away,
1027 * the return value will be %SOUP_SOCKET_WOULD_BLOCK. In this case,
1028 * the caller can connect to the %writable signal to know when more
1029 * data can be written. (NB: %writable is only emitted after a
1030 * %SOUP_SOCKET_WOULD_BLOCK.)
1032 * Return value: a #SoupSocketIOStatus, as described above (or
1033 * %SOUP_SOCKET_EOF or %SOUP_SOCKET_ERROR).
1036 soup_socket_write (SoupSocket *sock, gconstpointer buffer,
1037 gsize len, gsize *nwrote)
1040 gpointer pipe_handler;
1042 g_return_val_if_fail (SOUP_IS_SOCKET (sock), SOUP_SOCKET_ERROR);
1044 g_mutex_lock (sock->priv->iolock);
1046 if (!sock->priv->iochannel) {
1047 g_mutex_unlock (sock->priv->iolock);
1048 return SOUP_SOCKET_EOF;
1050 if (sock->priv->write_tag) {
1051 g_mutex_unlock (sock->priv->iolock);
1052 return SOUP_SOCKET_WOULD_BLOCK;
1055 pipe_handler = signal (SIGPIPE, SIG_IGN);
1056 status = g_io_channel_write_chars (sock->priv->iochannel,
1057 buffer, len, nwrote, NULL);
1058 signal (SIGPIPE, pipe_handler);
1059 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
1060 g_mutex_unlock (sock->priv->iolock);
1061 return SOUP_SOCKET_ERROR;
1065 g_mutex_unlock (sock->priv->iolock);
1066 return SOUP_SOCKET_OK;
1069 sock->priv->write_tag =
1070 g_io_add_watch (sock->priv->iochannel, G_IO_OUT,
1071 socket_write_watch, sock);
1072 g_mutex_unlock (sock->priv->iolock);
1073 return SOUP_SOCKET_WOULD_BLOCK;