[libsoupmm] Add form support



commit 92bddf0ed9d5be34362727490dd664c9223d071d
Author: Siavash Safi <siavash siavashs org>
Date:   Tue Sep 1 00:35:54 2009 +0430

    Add form support
    
    * libsoup/libsoupmm.h:
    * libsoup/libsoupmm/filelist.am: Fix a typo.
    * libsoup/libsoupmm/form.cc:
    * libsoup/libsoupmm/form.h: Hand code function to encode a form.
    * libsoup/src/message.ccg:
    * libsoup/src/message.hg: Upate hand coded create functions to return a
      0 RefPtr when the URI can't be created.
      Add create_form_request() which creates a Message containing a form
      request.

 libsoup/libsoupmm.h           |    2 +
 libsoup/libsoupmm/filelist.am |    4 +-
 libsoup/libsoupmm/form.cc     |   42 +++++++++++++++++++++++++++++++++++++++++
 libsoup/libsoupmm/form.h      |   29 ++++++++++++++++++++++++++++
 libsoup/src/message.ccg       |   41 ++++++++++++++++++++++++++++++++++++++-
 libsoup/src/message.hg        |    1 +
 6 files changed, 115 insertions(+), 4 deletions(-)
---
diff --git a/libsoup/libsoupmm.h b/libsoup/libsoupmm.h
index 6bdb1d8..24d4357 100644
--- a/libsoup/libsoupmm.h
+++ b/libsoup/libsoupmm.h
@@ -26,10 +26,12 @@
 #include <libsoupmm/address.h>
 #include <libsoupmm/auth.h>
 #include <libsoupmm/enums.h>
+#include <libsoupmm/form.h>
 #include <libsoupmm/message.h>
 #include <libsoupmm/message-body.h>
 #include <libsoupmm/session.h>
 #include <libsoupmm/status.h>
 #include <libsoupmm/uri.h>
+#include <libsoupmm/xmlrpc.h>
 
 #endif /* !LIBSOUPMM_H */
diff --git a/libsoup/libsoupmm/filelist.am b/libsoup/libsoupmm/filelist.am
index 389534b..654dece 100644
--- a/libsoup/libsoupmm/filelist.am
+++ b/libsoup/libsoupmm/filelist.am
@@ -3,6 +3,6 @@
 files_built_cc = $(files_hg:.hg=.cc) wrap_init.cc
 files_built_h  = $(files_hg:.hg=.h)
 files_built_ph = $(patsubst %.hg,private/%_p.h,$(files_hg))
-files_extra_cc = status.cc xmlrpc.cc
-files_extra_h  = status.h wrap_init.h xmlrpc.cc
+files_extra_cc = form.cc status.cc xmlrpc.cc
+files_extra_h  = form.h status.h wrap_init.h xmlrpc.h
 files_extra_ph =
diff --git a/libsoup/libsoupmm/form.cc b/libsoup/libsoupmm/form.cc
new file mode 100644
index 0000000..9d02476
--- /dev/null
+++ b/libsoup/libsoupmm/form.cc
@@ -0,0 +1,42 @@
+/* Copyright (c) 2009  Siavash Safi <siavashs siavashs org>
+ *
+ * This file is part of libsoupmm.
+ *
+ * libsoupmm is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 2.1 of the License,
+ * or (at your option) any later version.
+ *
+ * libsoupmm is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glibmm.h>
+#include <libsoupmm/form.h>
+#include <libsoup/soup-form.h>
+
+namespace Soup
+{
+
+std::string form_encode(const std::map<std::string, std::string>& fields)
+{
+  std::string encoded;
+
+  for(std::map<std::string, std::string>::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+  {
+    if(!encoded.empty())
+      encoded += "&";
+
+    encoded += iter->first + "=" + iter->second;
+  }
+
+  return encoded;
+}
+
+} // namespace Soup
+
diff --git a/libsoup/libsoupmm/form.h b/libsoup/libsoupmm/form.h
new file mode 100644
index 0000000..1d59455
--- /dev/null
+++ b/libsoup/libsoupmm/form.h
@@ -0,0 +1,29 @@
+/* Copyright (c) 2009  Siavash Safi <siavashs siavashs org>
+ *
+ * This file is part of libsoupmm.
+ *
+ * libsoupmm is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 2.1 of the License,
+ * or (at your option) any later version.
+ *
+ * libsoupmm is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBSOUPMM_FORM_H_INCLUDED
+#define LIBSOUPMM_FORM_H_INCLUDED
+
+namespace Soup
+{
+
+std::string form_encode(const std::map<std::string, std::string>& fields);
+
+} // namespace Soup
+
+#endif /* !LIBSOUPMM_FORM_H_INCLUDED */
diff --git a/libsoup/src/message.ccg b/libsoup/src/message.ccg
index c61b8f8..738de22 100644
--- a/libsoup/src/message.ccg
+++ b/libsoup/src/message.ccg
@@ -16,6 +16,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <libsoup/soup-form.h>
+#include <libsoupmm/form.h>
 #include <libsoupmm/xmlrpc.h>
 
 namespace Soup
@@ -24,7 +26,42 @@ namespace Soup
 Glib::RefPtr<Message>
 Message::create(const std::string& method, const std::string& uri_string)
 {
-  return create(method, URI(uri_string));
+  URI uri(uri_string);
+  if(!uri.gobj())
+    return Glib::RefPtr<Message>(0);
+
+  return create(method, uri);
+}
+
+Glib::RefPtr<Message>
+Message::create_form_request(const std::string& method, const std::string& uri_string, const std::map<std::string, std::string>& fields)
+{
+  URI uri(uri_string);
+  if(!uri.gobj())
+    return Glib::RefPtr<Message>(0);
+
+  std::string form_data = form_encode(fields);
+
+  if(method == "GET")
+  {
+    uri.set_query(form_data);
+    form_data.erase();
+  }
+
+  Glib::RefPtr<Message> msg = create(method, uri);
+
+  if((method == "POST") || method == "PUT")
+  {
+    msg->set_request(SOUP_FORM_MIME_TYPE_URLENCODED, MEMORY_COPY, form_data);
+    form_data.erase();
+  }
+
+  if(!form_data.empty())
+  {
+    g_warning ("invalid method passed to Soup::Message::create_form()");
+  }
+
+  return msg;
 }
 
 Glib::RefPtr<Message>
@@ -35,7 +72,7 @@ Message::create_xmlrpc_request(const std::string& uri_string, const std::string&
     return Glib::RefPtr<Message>(0);
 
   Glib::RefPtr<Message> msg = create("POST", uri_string);
-  msg->set_request("text/xml", Soup::MEMORY_COPY, body);
+  msg->set_request("text/xml", MEMORY_COPY, body);
   return msg;
 }
 
diff --git a/libsoup/src/message.hg b/libsoup/src/message.hg
index 15d92fc..42abdac 100644
--- a/libsoup/src/message.hg
+++ b/libsoup/src/message.hg
@@ -41,6 +41,7 @@ protected:
 public:
   _WRAP_CREATE(const std::string& method, const URI& uri)
   static Glib::RefPtr<Message> create(const std::string& method, const std::string& uri_string);
+  static Glib::RefPtr<Message> create_form_request(const std::string& method, const std::string& uri_string, const std::map<std::string, std::string>& fields);
   static Glib::RefPtr<Message> create_xmlrpc_request(const std::string& uri_string, const std::string& method_name, const std::list<Glib::ValueBase>& params);
 
   // TODO: Check if all types of MemoryUse work, e.g. what happens when we use MEMORY_TAKE ?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]