]> sjero.net Git - wget/blobdiff - src/gnutls.c
Make wgnutls_peek non blocking.
[wget] / src / gnutls.c
index 440b1aed2de7c429d6b0f36cda97804378d4efa3..4f38aca1d9c43ed302159d3ecf29c0e8f44fe29b 100644 (file)
@@ -1,5 +1,5 @@
 /* SSL support via GnuTLS library.
-   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
    Foundation, Inc.
 
 This file is part of GNU Wget.
@@ -32,9 +32,7 @@ as that of the covered work.  */
 
 #include <assert.h>
 #include <errno.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 #include <dirent.h>
@@ -42,6 +40,7 @@ as that of the covered work.  */
 
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
+#include <fcntl.h>
 
 #include "utils.h"
 #include "connect.h"
@@ -104,7 +103,8 @@ ssl_init ()
   return true;
 }
 
-struct wgnutls_transport_context {
+struct wgnutls_transport_context
+{
   gnutls_session session;       /* GnuTLS session handle */
   int last_error;               /* last error returned by read/write/... */
 
@@ -144,6 +144,7 @@ wgnutls_read (int fd, char *buf, int bufsize, void *arg)
 
   if (ret < 0)
     ctx->last_error = ret;
+
   return ret;
 }
 
@@ -182,6 +183,15 @@ wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
 
   if (bufsize > offset)
     {
+      int flags;
+      flags = fcntl (fd, F_GETFL, 0);
+      if (flags < 0)
+        return ret;
+
+      ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
+      if (ret < 0)
+        return ret;
+
       do
         {
           ret = gnutls_record_recv (ctx->session, buf + offset,
@@ -190,7 +200,12 @@ wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
       while (ret == GNUTLS_E_INTERRUPTED);
 
       if (ret < 0)
-        return ret;
+        {
+          if (offset)
+            ret = 0;
+          else
+            return ret;
+        }
 
       if (ret > 0)
         {
@@ -198,6 +213,10 @@ wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
                   ret);
           ctx->peeklen += ret;
         }
+
+      fcntl (fd, F_SETFL, flags);
+      if (ret < 0)
+        return ret;
     }
 
   return offset + ret;
@@ -223,7 +242,8 @@ wgnutls_close (int fd, void *arg)
 /* gnutls_transport is the singleton that describes the SSL transport
    methods provided by this file.  */
 
-static struct transport_implementation wgnutls_transport = {
+static struct transport_implementation wgnutls_transport =
+{
   wgnutls_read, wgnutls_write, wgnutls_poll,
   wgnutls_peek, wgnutls_errstr, wgnutls_close
 };