From 398117c4d06ad278b2c9c2827f8fafcbcdf26580 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 19 May 2017 12:20:38 -0400
Subject: [PATCH 13/31] FIXME: introduce struct file_descriptor

---
 gcc/http-server.c | 29 +++++++++++++++--------------
 gcc/http-server.h | 16 ++++++++++++----
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/gcc/http-server.c b/gcc/http-server.c
index a6b8ff1..4c1fd1f 100644
--- a/gcc/http-server.c
+++ b/gcc/http-server.c
@@ -120,7 +120,7 @@ server::serve (int port)
 	    else
 	      {
 		/* Data arriving on an already-connected socket. */
-		if (read_from_client (i) < 0)
+		if (read_from_client (file_descriptor (i)) < 0)
 		  {
 		    close (i);
 		    FD_CLR (i, &active_fd_set);
@@ -131,12 +131,12 @@ server::serve (int port)
 }
 
 int
-server::read_from_client (int filedes)
+server::read_from_client (file_descriptor fd)
 {
   char buffer[MAXMSG];
   int nbytes;
 
-  nbytes = read (filedes, buffer, MAXMSG);
+  nbytes = read (fd.m_fd, buffer, MAXMSG);
   if (nbytes < 0)
     {
       /* Read error. */
@@ -149,7 +149,7 @@ server::read_from_client (int filedes)
   else
     {
       /* Data read. */
-      on_read (filedes, nbytes, buffer);
+      on_read (fd, nbytes, buffer);
       return 0;
     }
 }
@@ -185,15 +185,16 @@ http::response::to_str () const
 }
 
 void
-http::server::on_read (int fd, size_t sz, const char *buf)
+http::server::on_read (file_descriptor fd, size_t length, const char *buf)
 {
   if (m_verbose)
-    fprintf (stderr, "httpish_server::on_read: `%s'\n", buf); // FIXME: respect sz
+    inform (UNKNOWN_LOCATION, "received http request: %qs",
+	    buf); // FIXME respect length
   size_t line_start = 0;
-  while (line_start < sz)
+  while (line_start < length)
     {
       request req;
-      for (size_t i = line_start; i + 1 < sz; i++)
+      for (size_t i = line_start; i + 1 < length; i++)
 	{
 	  if (buf[i] == '\r' && buf[i + 1] == '\n')
 	    {
@@ -201,7 +202,7 @@ http::server::on_read (int fd, size_t sz, const char *buf)
 		{
 		  line_start = i + 2;
 		  // FIXME: respect the "Content-Length" header
-		  req.set_content (sz - line_start, buf + line_start);
+		  req.set_content (length - line_start, buf + line_start);
 
 		  http::response resp;
 		  on_request (req, resp);
@@ -210,7 +211,7 @@ http::server::on_read (int fd, size_t sz, const char *buf)
 		  if (1)
 		    inform (UNKNOWN_LOCATION, "sending http response: %qs",
 			    resp_str);
-		  write (fd, resp_str, strlen (resp_str));
+		  write (fd.m_fd, resp_str, strlen (resp_str));
 		  free (resp_str);
 		  //line_start += ;
 		  return;
@@ -228,14 +229,14 @@ http::server::on_read (int fd, size_t sz, const char *buf)
 }
 
 void
-http::server::parse_header (int fd, size_t sz, const char *buf)
+http::server::parse_header (file_descriptor fd, size_t length, const char *buf)
 {
-  for (size_t colon_idx = 0; colon_idx + 1 < sz; colon_idx++)
+  for (size_t colon_idx = 0; colon_idx + 1 < length; colon_idx++)
     if (buf[colon_idx] == ':' && buf[colon_idx + 1] == ' ')
       {
 #if 0
 	on_header (fd, colon_idx, buf,
-		   sz - (colon_idx + 2), buf + colon_idx +2);
+		   length - (colon_idx + 2), buf + colon_idx +2);
 #endif
 	return;
       }
@@ -324,7 +325,7 @@ test_http_server ()
 		    "User-Agent: test-user-agent\r\n"
 		    "\r\n"
 		    "test-content");
-  s.on_read (0, strlen (in), in);
+  s.on_read (file_descriptor (2), strlen (in), in);
 #if 0
   ASSERT_STREQ ("test-user-agent", s.m_user_agent);
   ASSERT_STREQ ("test-content", s.m_content);
diff --git a/gcc/http-server.h b/gcc/http-server.h
index 888f25c..d53081a 100644
--- a/gcc/http-server.h
+++ b/gcc/http-server.h
@@ -20,6 +20,13 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_HTTP_SERVER_H
 #define GCC_HTTP_SERVER_H
 
+struct file_descriptor
+{
+  explicit file_descriptor (int fd) : m_fd (fd) {}
+
+  int m_fd;
+};
+
 /* FIXME.  */
 
 class server
@@ -28,10 +35,10 @@ class server
   virtual ~server () {}
   void serve (int port);
 
-  virtual void on_read (int fd, size_t sz, const char *buf) = 0;
+  virtual void on_read (file_descriptor fd, size_t length, const char *buf) = 0;
 
  private:
-  int read_from_client (int filedes);
+  int read_from_client (file_descriptor fd);
 };
 
 namespace http {
@@ -73,13 +80,14 @@ class server : public ::server
  public:
   server (bool verbose) : m_verbose (verbose) {}
 
-  void on_read (int fd, size_t sz, const char *buf) OVERRIDE FINAL;
+  void on_read (file_descriptor fd, size_t length,
+		const char *buf) OVERRIDE FINAL;
 
   virtual void on_request (const http::request &request,
 			   http::response &response) = 0;
 
  private:
-  void parse_header (int fd, size_t sz, const char *buf);
+  void parse_header (file_descriptor fd, size_t sz, const char *buf);
 
  private:
   bool m_verbose;
-- 
1.8.5.3

