Boost logo

Boost-Commit :

From: lists.drrngrvy_at_[hidden]
Date: 2008-03-31 16:08:25


Author: drrngrvy
Date: 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
New Revision: 43975
URL: http://svn.boost.org/trac/boost/changeset/43975

Log:
Modified CGI stuff to not use std::cout/cin/cerr, removing the dependence on <iostream> and reducing binary size a fair bit (seemingly making things faster too).
Text files modified:
   sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp | 18 +++++
   sandbox/SOC/2007/cgi/trunk/boost/cgi/connections/stdio.hpp | 36 ++++++++++-
   sandbox/SOC/2007/cgi/trunk/boost/cgi/data_source.hpp | 14 +++-
   sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_request_impl_base.hpp | 2
   sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_service_impl_base.hpp | 120 +++++++++++++++++++--------------------
   sandbox/SOC/2007/cgi/trunk/boost/cgi/error.hpp | 9 ++
   6 files changed, 124 insertions(+), 75 deletions(-)

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -16,7 +16,6 @@
 
 #include "boost/cgi/detail/push_options.hpp"
 
-#include <iostream>
 #include <boost/noncopyable.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/assert.hpp>
@@ -29,7 +28,7 @@
 #include "boost/cgi/detail/protocol_traits.hpp"
 #include "boost/cgi/request_base.hpp"
 #include "boost/cgi/role_type.hpp"
-#include "boost/cgi/data_sink.hpp"
+#include "boost/cgi/data_source.hpp"
 #include "boost/cgi/status_type.hpp"
 #include "boost/cgi/is_async.hpp"
 #include "boost/cgi/connection_base.hpp"
@@ -648,6 +647,21 @@
     {
       return this->implementation.boundary_marker;
     }
+
+ map_type& operator[](common::data_source source)
+ {
+ switch(source)
+ {
+ case get_data: return this->implementation.get_vars_;
+ case post_data: return this->implementation.post_vars_;
+ case cookie_data: return this->implementation.cookie_vars_;
+ case env_data: return this->implementation.env_vars_;
+ case form_data:
+ std::string rm( request_method() );
+ if (rm == "GET") return this->implementation.get_vars_;
+ else if (rm == "POST") return this->implementation.post_vars_;
+ }
+ }
   };
 
  } // namespace common

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/connections/stdio.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/connections/stdio.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/connections/stdio.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -9,9 +9,7 @@
 #ifndef CGI_STDIO_CONNECTION_IMPL_HPP_INCLUDED__
 #define CGI_STDIO_CONNECTION_IMPL_HPP_INCLUDED__
 
-#include <iostream>
-#include <istream>
-#include <ostream>
+#include <cstdio>
 #include <string>
 #include <boost/system/error_code.hpp>
 #include <boost/asio.hpp>
@@ -19,7 +17,7 @@
 #include "boost/cgi/basic_connection_fwd.hpp"
 #include "boost/cgi/tags.hpp"
 #include "boost/cgi/connection_base.hpp"
-#include "boost/cgi/data_sink.hpp"
+#include "boost/cgi/error.hpp"
 //#include "boost/cgi/io_service.hpp"
 
 namespace cgi {
@@ -75,6 +73,7 @@
       // << "before = {" << std::endl
       // << std::string(boost::asio::buffer_cast<char *>(buf), boost::asio::buffer_size(buf)) << std::endl
       // << "}" << std::endl;
+ /*
       std::cin.read(boost::asio::buffer_cast<char *>(buf)
                    , boost::asio::buffer_size(buf));
       if (std::cin.fail() && !std::cin.eof())
@@ -87,6 +86,24 @@
       // << std::string(boost::asio::buffer_cast<char *>(buf), boost::asio::buffer_size(buf)) << std::endl
       // << "}" << std::endl;
       return std::cin.gcount();
+ */
+ if (!std::fgets(boost::asio::buffer_cast<char *>(buf)
+ , boost::asio::buffer_size(buf)
+ , stdin))
+ {
+ return ::cgi::error::bad_read;
+ }
+ int len( strlen(boost::asio::buffer_cast<char *>(buf)) );
+ // Not sure what to do about EOF yet.
+ //if (len < boost::asio::buffer_size(buf))
+ //{
+ // return ::cgi::error::eof;
+ //}
+ std::cerr<< "Read data" << std::endl
+ << "after = {" << std::endl
+ << std::string(boost::asio::buffer_cast<char *>(buf), boost::asio::buffer_size(buf)) << std::endl
+ << "}" << std::endl;
+ return len;
     }
 
     template<typename ConstBufferSequence>
@@ -99,7 +116,16 @@
       {
         std::size_t buf_len = boost::asio::buffer_size(*i);
         bytes_transferred += buf_len;
- std::cout.write(boost::asio::buffer_cast<const char*>(*i), buf_len);
+ int ret( fputs(boost::asio::buffer_cast<const char*>(*i), stdout) );
+ if (ret == EOF)
+ {
+ return ::cgi::error::broken_pipe;
+ }
+ //else
+ //if (ret < 0)
+ //{
+ // return ::cgi::error::
+ //std::cout.write(boost::asio::buffer_cast<const char*>(*i), buf_len);
       }
       return bytes_transferred;
     }

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/data_source.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/data_source.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/data_source.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -14,11 +14,17 @@
   enum source
   { stdin_ };
 
- namespace data_source {
+ namespace common {
 
- //struct stdin_ {};
-
- } // namespace data_source
+ enum data_source
+ { get_data
+ , post_data
+ , cookie_data
+ , env_data
+ , form_data
+ };
+
+ } // namespace common
 } // namespace cgi
 
 #endif // CGI_DATA_SOURCE_HPP_INCLUDED__

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_request_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_request_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_request_impl_base.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -68,7 +68,7 @@
     conn_ptr& connection() { return connection_; }
     //std::string& null_str() { return null_str_; }
 
- protected:
+ public:
     //conn_ptr connection() { return connection_; }
 
     //friend class cgi_service_impl_base<RequestImpl>;

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_service_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_service_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/detail/cgi_service_impl_base.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -5,7 +5,6 @@
 
 #include <string>
 #include <cstdlib>
-#include <iostream>
 #include <boost/assert.hpp>
 #include <boost/regex.hpp>
 #include <boost/tokenizer.hpp>
@@ -31,9 +30,6 @@
 
  } // namespace detail
 
- using std::cerr; // **FIXME**
- using std::endl; // **FIXME**
-
 
   template<typename RequestImplType>
   class cgi_service_impl_base
@@ -110,7 +106,7 @@
       {
         
         std::size_t bufsz( buf_.size() );
- cerr<< "bufsz = " << bufsz << endl;
+ //cerr<< "bufsz = " << bufsz << endl;
 
         // Reserve more space if it's needed.
         // (this could be safer, referencing this against CONTENT_LENGTH)
@@ -119,13 +115,13 @@
           buf_.resize(bufsz + size);
         //}
 
- cerr<< "Pre-read buffer (size: " << buf_.size()
- << "|capacity: " << buf_.capacity() << ") == {" << endl
- << std::string(buf_.begin() + offset_, buf_.end()) << endl
+ //cerr<< "Pre-read buffer (size: " << buf_.size()
+ // << "|capacity: " << buf_.capacity() << ") == {" << endl
+ // << std::string(buf_.begin() + offset_, buf_.end()) << endl
    // << "-----end buffer-----" << endl
    // << "-------buffer-------" << endl
   // << std::string(&buf_[0], &buf_[buf_.size()]) << endl
- << "}" << endl;
+ // << "}" << endl;
             ;
         //return boost::asio::buffer(&(*(buf_.end())), size);
   // return boost::asio::buffer(&(*(buf_.begin())) + bufsz, size);
@@ -473,7 +469,7 @@
       //parse_one_form_part(impl, ec);
       move_to_start_of_first_part(impl, ec);
       if (ec == boost::asio::error::eof) {
- cerr<< " -- Parsing done -- " << endl;
+ //cerr<< " -- Parsing done -- " << endl;
         //return ec.clear();
         return boost::system::error_code();
       }
@@ -519,7 +515,7 @@
       }
       
       regex += ")(--)?[ ]*\\x0D\\x0A";
- cerr<< "Regex: " << regex << endl;
+ //cerr<< "Regex: " << regex << endl;
       boost::regex re(regex);
       
       typedef typename
@@ -529,7 +525,7 @@
       boost::match_results<buffer_iter> matches;
 
       std::size_t offset = impl.offset_;
- cerr<< "offset = " << offset << endl;
+ //cerr<< "offset = " << offset << endl;
 
       //int runs = 0;
       buffer_iter begin(impl.buf_.begin() + offset);
@@ -537,14 +533,14 @@
 
       for(;;)
       {
- cerr<< "Starting regex_search" << endl;
+ //cerr<< "Starting regex_search" << endl;
         if (!boost::regex_search(begin, end, matches, re
                                 , boost::match_default
                                 | boost::match_partial))
         {
- cerr<< "Can't match any of this. {" << endl
- << std::string(begin, end) << endl
- << "}" << endl;
+ // cerr<< "Can't match any of this. {" << endl
+ // << std::string(begin, end) << endl
+ // << "}" << endl;
           return boost::system::error_code(345, boost::system::system_category);
         }
         else
@@ -553,9 +549,9 @@
           {
             if (matches[i].length())
             {
- cerr<< "[" << i << "] == {" << endl
- << matches[i] << endl
- << "}" << endl;
+ // cerr<< "[" << i << "] == {" << endl
+ // << matches[i] << endl
+ // << "}" << endl;
             }
           }
           // cerr<< "matches[0] = {" << endl
@@ -566,15 +562,15 @@
             impl.form_parts_.back().buffer_
              // = boost::range_iterator<;
              = std::make_pair(matches[1].first, matches[1].second);
- cerr<< "Saved buffer (size: "
- << std::distance(matches[1].first, matches[1].second)
- << ") := { " << impl.form_parts_.back().name << ", " << matches[1] << " }" << endl;
+ // cerr<< "Saved buffer (size: "
+ // << std::distance(matches[1].first, matches[1].second)
+ // << ") := { " << impl.form_parts_.back().name << ", " << matches[1] << " }" << endl;
             impl.post_vars()[impl.form_parts_.back().name] = matches[1];
             impl.offset_ = offset + matches[0].length();
             //offset += matches[0].length();
             impl.pos_ = matches[0].second;
- cerr<< "offset := " << offset << endl
- << "impl.offset_ := " << impl.offset_ << endl;
+ //cerr<< "offset := " << offset << endl
+ // << "impl.offset_ := " << impl.offset_ << endl;
 
             if (matches[3].matched)
               impl.stdin_parsed_ = true;
@@ -587,7 +583,7 @@
           }
           else
           {
- cerr<< "Reading more data." << endl;
+ //cerr<< "Reading more data." << endl;
             std::size_t bytes_read = impl.client_.read_some(impl.prepare(64), ec);
             //impl.stdin_bytes_read_ += bytes_read;
             
@@ -599,13 +595,13 @@
 
             begin = impl.buf_.begin() + offset;
             end = impl.buf_.end();
- cerr<< "Buffer (+" << bytes_read << ") == {" << endl
- << std::string(begin, end) << endl
- << "}" << endl;
+ //cerr<< "Buffer (+" << bytes_read << ") == {" << endl
+ // << std::string(begin, end) << endl
+ // << "}" << endl;
 
             if (ec)
             {
- cerr<< "Error in parse_form_part_data()." << endl;
+ //cerr<< "Error in parse_form_part_data()." << endl;
               return ec;
             }
 
@@ -678,10 +674,10 @@
> matches;
       
       std::size_t offset = impl.offset_;
- cerr.flush();
+ //cerr.flush();
       impl.pos_ = impl.buf_.begin();
       int runs = 0;
- cerr<< "Entering for() loop." << endl;
+ //cerr<< "Entering for() loop." << endl;
       std::size_t bytes_read = 0;
       for(;;)
       {
@@ -691,18 +687,18 @@
         if (!boost::regex_search(begin, end, matches, re
                                 , boost::match_default | boost::match_partial))
         {
- cerr<< "No chance of a match, quitting." << endl;
+ //cerr<< "No chance of a match, quitting." << endl;
           impl.stdin_parsed_ = true;
           return ec;
         }
- cerr<< "matches.str() == {" << endl
- << matches.str() << endl
- << "}" << endl
- << matches.size() << " submatches" << endl;
- for (unsigned i = matches.size(); i != 0; --i)
- {
- cerr<< "match[" << i << "] := { " << matches[i] << " }" << endl;
- }
+ //cerr<< "matches.str() == {" << endl
+ // << matches.str() << endl
+ // << "}" << endl
+ // << matches.size() << " submatches" << endl;
+ // for (unsigned i = matches.size(); i != 0; --i)
+ // {
+ //cerr<< "match[" << i << "] := { " << matches[i] << " }" << endl;
+ //}
         if (matches[0].matched)
         {
           common::form_part part;
@@ -715,13 +711,13 @@
            if (matches[i].str() == "name")
             {
               part.name = matches[i+1];
- cerr<< "Saved name" << endl;
+ // cerr<< "Saved name" << endl;
             }
             else
             {
               part.meta_data_[matches[i]]
                 = std::make_pair(matches[i+1].first, matches[i+1].second);
- cerr<< "Part := { " << matches[i] << ", " << matches[i+1] << " }" << endl;
+ // cerr<< "Part := { " << matches[i] << ", " << matches[i+1] << " }" << endl;
               //= boost::iterator_range<buffer_iter>(matches[3].first, matches[3].second);
             }
             impl.form_parts_.push_back(part);
@@ -738,7 +734,7 @@
            //cerr<< "Current buffer == {" << endl
            // << impl.buffer_string() << endl
            // << "}" << endl;
- cerr<< "Leaving parse_form_part_meta_data()" << endl;
+ //cerr<< "Leaving parse_form_part_meta_data()" << endl;
            return ec;
          }
          else
@@ -747,18 +743,18 @@
          }
          
         }else{
- cerr<< "Not read enough data yet, reading more." << endl;
+ // cerr<< "Not read enough data yet, reading more." << endl;
           bytes_read = impl.client_.read_some(impl.prepare(64), ec);
           if (ec)
           {
- cerr<< "Error reading data: " << ec.message() << endl;
- cerr<< "Leaving parse_form_part_meta_data()" << endl;
+ // cerr<< "Error reading data: " << ec.message() << endl;
+ // cerr<< "Leaving parse_form_part_meta_data()" << endl;
             return ec;
           }
- cerr<< "Read " << bytes_read << " bytes." << endl;
- cerr<< "buffer = {" << endl
- << impl.buffer_string() << endl
- << "} or {" << endl;
+ // cerr<< "Read " << bytes_read << " bytes." << endl;
+ //cerr<< "buffer = {" << endl
+ // << impl.buffer_string() << endl
+ // << "} or {" << endl;
               //<< std::string(impl.pos_, ;
           /*
           for (unsigned int i = 0; i < matches.size(); ++i)
@@ -777,13 +773,13 @@
           //offset = impl.buf_.end();
             if (++runs > 40)
             {
- cerr<< "Run 40 times; bailing out." << endl;
+ // cerr<< "Run 40 times; bailing out." << endl;
               break;
             }
- cerr<< "Waiting buffer (unparsed) == {" << endl << std::flush
- << impl.buffer_string() << endl
- << "}" << endl
- << "offset = " << offset << endl;
+ // cerr<< "Waiting buffer (unparsed) == {" << endl << std::flush
+ // << impl.buffer_string() << endl
+ // << "}" << endl
+ // << "offset = " << offset << endl;
         //if (bytes_read == 0)
         // break;
        }
@@ -791,7 +787,7 @@
 
       //cerr<< "impl.part
 
- cerr<< "Leaving parse_form_part_meta_data()" << endl;
+ // cerr<< "Leaving parse_form_part_meta_data()" << endl;
       return ec;
     }
 
@@ -843,7 +839,7 @@
           //std::cerr<< " == buffer = " << std::string(impl.buf_.begin(), impl.buf_.end())
           // << " == capacity = " << impl.buf_.capacity() << " ======= ";
           //impl.buf_.clear();
- cerr<< "No chance of matching." << endl;
+ // cerr<< "No chance of matching." << endl;
           offset = impl.buf_.size();
           //std::cerr<< "Buffer cleared." << endl;
           continue;
@@ -855,7 +851,7 @@
           // << "matches[1] =={{ " << matches[1] << " }}=== " << std::endl
           // << "matches[2] =={{ " << matches[2] << " }}=== " << std::endl;
           if (matches[2].matched){
- cerr<< "Found boundary marker... OK!!" << endl;
+ // cerr<< "Found boundary marker... OK!!" << endl;
             //cerr<< "[0] = " << matches[0].str() << endl;
             //cerr<< "[1] = " << matches[1].str() << endl;
             //impl.offset_ = matches[1].length();
@@ -871,15 +867,15 @@
             // << "}" << endl;
             //cerr<< "bufsize = " << impl.buf_.size() << endl;
             //cerr<< "bufsize = " << impl.buf_.size() << endl;
- cerr<< "buffer now (before erase) == {" << endl
- << std::string(impl.buf_.begin(), impl.buf_.end()) << endl
- << "}" << endl;
+ // cerr<< "buffer now (before erase) == {" << endl
+ // << std::string(impl.buf_.begin(), impl.buf_.end()) << endl
+ // << "}" << endl;
             impl.buf_.erase(impl.buf_.begin(), matches[0].second);
             impl.offset_ = 0;
             impl.pos_ = impl.buf_.begin();
             return ec;
           } else {
- cerr<< "not read enough data" << std::endl;
+ // cerr<< "not read enough data" << std::endl;
       //std::cerr<< "; bytes_read = " << bytes_read
         // << "; bufsize = " << impl.buf_.size()
           // << "; capacity = " << impl.buf_.capacity() << std::flush

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/error.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/error.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/error.hpp 2008-03-31 16:08:24 EDT (Mon, 31 Mar 2008)
@@ -50,7 +50,14 @@
   // allowed.
   accepting_on_an_open_request,
 
- invalid_socket
+ invalid_socket,
+
+ // Used in basic_connection<tags::stdio>
+ // **FIXME**
+ broken_pipe,
+
+ // **FIXME**
+ bad_read
 };
 
   namespace detail {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk