OSDN Git Service

base64, mine, uri/url もとりあえず復活か
authorMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 10:52:42 +0000 (19:52 +0900)
committerMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 10:52:42 +0000 (19:52 +0900)
roast/include/roast/net/base64.hpp [new file with mode: 0644]
roast/include/roast/net/base64_enc_impl.hpp [new file with mode: 0644]
roast/include/roast/net/http/http.hpp
roast/include/roast/net/mime.hpp [new file with mode: 0644]
roast/include/roast/net/uri.hpp [new file with mode: 0644]
roast/include/roast/net/url.hpp [new file with mode: 0644]
roast/include/roast/net/urlenc.hpp [new file with mode: 0644]

diff --git a/roast/include/roast/net/base64.hpp b/roast/include/roast/net/base64.hpp
new file mode 100644 (file)
index 0000000..3329f3a
--- /dev/null
@@ -0,0 +1,92 @@
+//     Roast+ License
+
+/*
+*/
+#ifndef __SFJP_ROAST__roast__net__base64_HPP__
+#define __SFJP_ROAST__roast__net__base64_HPP__
+
+//#include "roast_base64.h"
+#include "roast/web/base64_enc_impl.hpp"
+#include <stdio.h>
+#include <string>
+
+
+namespace roast
+{
+       template <int _LF_BY = -1>
+       class base64_enc_
+       {
+       private:
+               //::std::string m_result;
+               char* m_result;
+
+               void _analyze_main(const char* s, size_t length)
+               {
+                       ::roast_base64_enc(s, length, m_result);
+               }
+       public:
+               base64_enc_() {
+                       m_result = NULL;
+               }
+               base64_enc_(const char* s, size_t length=0) {
+                       analyze(s,length);
+               }
+               base64_enc_(const ::std::string &s) {
+                       analyze(s);
+               }
+               virtual ~base64_enc_(){
+                       if ( m_result != NULL )
+                               delete[] m_result;
+               }
+
+               ///////////////////////////////////////////////////
+
+               void analyze(const ::std::string &s){
+                       analyze( s.c_str(), s.length() );
+               }
+               void analyze(const char* s, size_t length=0)
+               {
+                       if ( length == 0 )
+                               length = strlen(s);
+
+                       //size_t result_length = length + length/3 + 2;
+                       size_t result_length = length + length/3 + length/5 + 3;
+                       //result_length
+                       //printf("%d\n", result_length);
+                       m_result = new char [result_length];
+
+                       _analyze_main(s,length);
+               }
+
+               operator const char*()
+               {
+                       return m_result;
+               }
+
+               const char* c_str()
+               {
+                       return m_result;
+               }
+
+               const char* get_result()
+               {
+                       return m_result;
+               }
+
+               ///////////////////////////////////////////////////
+       };
+       typedef base64_enc_<> base64_enc;
+}
+
+/*
+//sample:
+
+void main()
+{
+       base64_enc be("abcabcab");
+       printf("Result:%s\n", be.get_result() );
+}
+
+*/
+
+#endif//__SFJP_ROAST__roast__net__base64_HPP__
diff --git a/roast/include/roast/net/base64_enc_impl.hpp b/roast/include/roast/net/base64_enc_impl.hpp
new file mode 100644 (file)
index 0000000..eefaa54
--- /dev/null
@@ -0,0 +1,89 @@
+//     Roast+ License
+
+/*
+*/
+#ifndef __SFJP_ROAST__roast__net__base64_impl_HPP__
+#define __SFJP_ROAST__roast__net__base64_impl_HPP__
+
+#include "roast_common.h"
+#include <stdio.h>
+
+#define _ROAST_BASE64_ENC_TABLE                "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
+
+static const char* _g_roast_base64_enc_table = _ROAST_BASE64_ENC_TABLE;
+
+#define _by_tbl(c)             _g_roast_base64_enc_table[c]
+
+/*
+       Base64 Encoding.
+       
+       s:          Input String.
+       length:     Input String Length.
+       result:     Result Buffer.
+                   Need Enough Buffer Size!! (Not checked buffer size)
+*/
+inline void roast_base64_enc(const char* s, size_t length, char* result)
+{
+       const char* p_in = s;
+       const char* s_end = s + length;
+       char *p_out = result;
+
+       for(; p_in < s_end; p_in += 3, p_out += 4)
+       {
+               int n = ROAST_REVERSE_ENDIAN_32( *((int*)p_in) );
+
+               //printf("%08X ", n);
+               /*
+               *p_out = _by_tbl( (n & 0xFC000000) >> (32-6) );
+               p_out++;
+               *p_out = _by_tbl( (n & 0x03F00000) >> (32-6*2) );
+               p_out++;
+               *p_out = _by_tbl( (n & 0x000FC000) >> (32-6*3) );
+               p_out++;
+               *p_out = _by_tbl( (n & 0x000F3F00) >> (32-6*4) );
+               p_out++;
+               */
+               
+               /*
+               int a = _by_tbl( (n & 0xFC000000) >> (32-6) );
+               int b = _by_tbl( (n & 0x03F00000) >> (32-6*2) );
+               p_out[0] = a;
+               p_out[1] = b;
+               a = _by_tbl( (n & 0x000FC000) >> (32-6*3) );
+               b = _by_tbl( (n & 0x000F3F00) >> (32-6*4) );
+               p_out[2] = a;
+               p_out[3] = b;
+               */
+               
+               int a = _by_tbl( (n & 0xFC000000) >> (32-6) );
+               int b = _by_tbl( (n & 0x03F00000) >> (32-6*2) );
+               int c = _by_tbl( (n & 0x000FC000) >> (32-6*3) );
+               int d = _by_tbl( (n & 0x00003F00) >> (32-6*4) );
+               p_out[0] = a;
+               p_out[1] = b;
+               p_out[2] = c;
+               p_out[3] = d;
+       }
+
+       /* padding = */
+       if ( (p_in - s_end) == 1 )
+       {
+               p_out -= 1;
+               *p_out = _by_tbl(64);
+               p_out++;
+       }
+       
+       /* padding == */
+       else if ( (p_in - s_end) == 2 )
+       {
+               p_out -= 2;
+               *p_out = _by_tbl(64);
+               p_out++;
+               *p_out = _by_tbl(64);
+               p_out++;
+       }
+
+       *p_out = '\0';
+}
+
+#endif//__SFJP_ROAST__roast__net__base64_impl_HPP__
index 3689ebf..102cbef 100644 (file)
@@ -2,12 +2,10 @@
 
 /*
 */
-#ifndef __SFJP_ROAST__web__http__http_HPP__
-#define __SFJP_ROAST__web__http__http_HPP__
+#ifndef __SFJP_ROAST__net__http__http_HPP__
+#define __SFJP_ROAST__net__http__http_HPP__
 
-#include "roast/net/telnet.hpp"
-#include "roast/web/base64.hpp"
-#include "roast/stream/stream.hpp"     //      for Debug Print
+#include "roast/net/base64.hpp"
 #include <string>
 #include <map>
 #include <vector>
@@ -269,4 +267,4 @@ namespace roast
        typedef http_<> http;
 }
 
-#endif//__SFJP_ROAST__web__http__http_HPP__
+#endif//__SFJP_ROAST__net__http__http_HPP__
diff --git a/roast/include/roast/net/mime.hpp b/roast/include/roast/net/mime.hpp
new file mode 100644 (file)
index 0000000..4074f80
--- /dev/null
@@ -0,0 +1,33 @@
+//     Roast+ License
+
+/*
+*/
+#ifndef __SFJP_ROAST__net__web__mime_HPP__
+#define __SFJP_ROAST__net__web__mime_HPP__
+
+namespace roast
+{
+       namespace mime
+       {
+               //      text/*
+               static const char* plain_text = "text/plain";
+               static const char* html = "text/html";
+               static const char* xml = "text/xml";
+               static const char* javascript = "text/javascript";
+               static const char* vbscript = "text/vbscript";
+               static const char* css = "text/css";
+               
+               //      image/*
+               static const char* gif = "image/gif";
+               static const char* jpg = "image/jpeg";
+               static const char* jpeg = "image/jpeg";
+               static const char* png = "image/png";
+
+               //      application/*
+               static const char* cgi = "application/x-httpd-cgi";
+               static const char* msword = "application/msword";
+               static const char* psf = "application/pdf";
+       }
+}
+
+#endif//__SFJP_ROAST__net__web__mime_HPP__
diff --git a/roast/include/roast/net/uri.hpp b/roast/include/roast/net/uri.hpp
new file mode 100644 (file)
index 0000000..4dff201
--- /dev/null
@@ -0,0 +1,77 @@
+//     Roast+ License
+
+/*
+       Uniform Resource Identifier
+*/
+#ifndef __SFJP_ROAST__web__uri_HPP__
+#define __SFJP_ROAST__web__uri_HPP__
+
+#include "roast/lexical.hpp"
+#include <string>
+
+namespace roast
+{
+       ///////////////////////////////////////////////////////////////////////////
+
+       // Syntax:
+       //      scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+       template <typename _Scheme = lexical::string, typename _Body = lexical::string>
+       class uri_ : public ::roast::lexical::seq<
+               _Scheme, ::roast::lexical::chars::colon, _Body>
+       {
+       private:
+               typedef ::roast::lexical::seq<
+                       _Scheme, ::roast::lexical::chars::colon, _Body> _Base;
+       public:
+               uri_(){}
+               uri_(const _Scheme& scheme_in, const _Body& body_in){
+                       scheme() = scheme_in;
+                       body() = body_in;
+               }
+               
+               _Scheme& scheme(){ return _Base::ref<0>(); }
+               const _Scheme& scheme() const { return _Base::ref<0>(); }
+               
+               _Body& body(){ return _Base::ref<2>(); }
+               const _Body& body() const { return _Base::ref<2>(); }
+       };
+       typedef uri_<> uri;
+               
+       ///////////////////////////////////////////////////////////
+       
+       // Syntax:
+       //      authority = [ userinfo "@" ] host [ ":" port ]
+       template <typename _Host, typename _UserInfo=EmptyType, typename _Port=EmptyType>
+       class uri_authority : public ::roast::lexical::seq<
+               _UserInfo, ::roast::lexical::chars::at, _Host, ::roast::lexical::chars::colon, _Port> {};
+       
+       //      authority : _UserInfo is Empty
+       template <typename _Host, typename _Port>
+       class uri_authority<_Host, EmptyType, _Port> : public ::roast::lexical::seq<
+               /*_UserInfo, ::roast::lexical::chars::at,*/ _Host, ::roast::lexical::chars::colon, _Port> {};
+       
+       //      authority : _Port is Empty
+       template <typename _Host, typename _UserInfo>
+       class uri_authority<_Host, _UserInfo, EmptyType> : public ::roast::lexical::seq<
+               _UserInfo, ::roast::lexical::chars::at, _Host/*, ::roast::lexical::chars::colon, _Port*/> {};
+       
+       //      authority : _UserInfo, _Port is Empty
+       template <typename _Host>
+       class uri_authority<_Host, EmptyType, EmptyType> : public _Host {};
+       
+       ///////////////////////////////////////////////////////////
+       
+       typedef ::roast::lexical::seq<
+               ::roast::lexical::chars::slash,
+               ::roast::lexical::chars::slash>
+               double_slash, dbl_slash, slashslash, slash_slash;
+
+       //      URI with authority.
+       template <typename _Scheme, typename _Authority, typename _Path>
+       class uri_with_authority : public uri_<
+               _Scheme, ::roast::lexical::seq< _Authority, _Path> > {};
+       
+       ///////////////////////////////////////////////////////////////////////////
+}
+
+#endif//__SFJP_ROAST__web__uri_HPP__
diff --git a/roast/include/roast/net/url.hpp b/roast/include/roast/net/url.hpp
new file mode 100644 (file)
index 0000000..866ed89
--- /dev/null
@@ -0,0 +1,70 @@
+//     Roast+ License
+
+/*
+*/
+#ifndef __SFJP_ROAST__web__url_HPP__
+#define __SFJP_ROAST__web__url_HPP__
+
+#include "roast/web/uri.hpp"
+#include <string>
+
+namespace roast
+{
+       //////////////////////////////////////////////////////////////////////
+
+       class url
+       {
+       private:
+               int port_no;
+               ::std::string host_s;
+               ::std::string path_s;
+       public:
+               url(){
+                       port_no = 0;
+               }
+               url(const char* url){ parse(url); }
+               url(const ::std::string& url){ parse(url.c_str()); }
+
+               void parse(const char* url)
+               {
+                       port_no = 80;
+                       //if ( strncmp(url, "http://") == 0 )
+                       const char* p_host = strstr(url,"://");
+                       if ( p_host == NULL )
+                               p_host = url;
+                       else
+                               p_host += 3;
+                       
+                       //const char* p_port_specify = strchr(
+                       const char* phost_slash = strchr(p_host,'/');
+                       if ( phost_slash == NULL )
+                       {
+                               host_s = p_host;
+                               path_s = "/";
+                       }
+                       else
+                       {
+                               host_s = ::std::string(p_host, phost_slash-p_host);
+                               //path = phost_slash+1;
+                               path_s = phost_slash;
+                       }
+                       
+                       const char* host_cstr = host_s.c_str();
+                       size_t n_port_colon = host_s.find(':');
+                       if ( n_port_colon != ::std::string::npos )
+                       {
+                               const char* p_port_colon = host_cstr + n_port_colon;
+                               port_no = atoi(p_port_colon+1);
+                               host_s.erase(p_port_colon - host_cstr);
+                       }
+               }
+               
+               int get_portno(){ return port_no; }
+               const char* get_host(){ return host_s.c_str(); }
+               const char* get_path(){ return path_s.c_str(); }
+       };
+       
+       //////////////////////////////////////////////////////////////////////
+}
+
+#endif//__SFJP_ROAST__web__url_HPP__
diff --git a/roast/include/roast/net/urlenc.hpp b/roast/include/roast/net/urlenc.hpp
new file mode 100644 (file)
index 0000000..4d0a490
--- /dev/null
@@ -0,0 +1,163 @@
+//     Roast+ License
+
+/*
+*/
+#ifndef __SFJP_ROAST__roast__net__urlenc_HPP__
+#define __SFJP_ROAST__roast__net__urlenc_HPP__
+
+#include "roast/str/codec.hpp"
+#include <string>
+
+
+#define ROAST_CHAR_TO_HEXSTR(C,STR,HEXSTART)   (\
+       (STR)[0] = '0' + C / 16,        \
+       (STR)[0] > '9' ? (STR)[0] += (HEXSTART-'9'-1) : 0,      \
+       (STR)[1] = '0' + C % 16,        \
+       (STR)[1] > '9' ? (STR)[1] += (HEXSTART-'9'-1) : 0,      \
+       STR)
+
+namespace roast
+{
+       template <bool _SpaceToPlus=true>
+       class url_encode_impl
+       {
+       private:
+               size_t m_wrote_length;
+       public:
+               typedef char _FromType;
+               typedef char _ToType;
+               
+               url_encode_impl(const _FromType* from, _ToType* to, size_t rest_length)
+               {
+                       unsigned char c = *from;
+                       
+                       switch(c)
+                       {
+                       case '.':
+                       case '-':
+                       case '_':
+                       
+                       //      '0'-'9'
+                       case    48      :
+                       case    49      :
+                       case    50      :
+                       case    51      :
+                       case    52      :
+                       case    53      :
+                       case    54      :
+                       case    55      :
+                       case    56      :
+                       case    57      :
+                       
+                       //      'A'-'Z'
+                       case    65      :
+                       case    66      :
+                       case    67      :
+                       case    68      :
+                       case    69      :
+                       case    70      :
+                       case    71      :
+                       case    72      :
+                       case    73      :
+                       case    74      :
+                       case    75      :
+                       case    76      :
+                       case    77      :
+                       case    78      :
+                       case    79      :
+                       case    80      :
+                       case    81      :
+                       case    82      :
+                       case    83      :
+                       case    84      :
+                       case    85      :
+                       case    86      :
+                       case    87      :
+                       case    88      :
+                       case    89      :
+                       
+                       //      'a'-'z'
+                       case    97      :
+                       case    98      :
+                       case    99      :
+                       case    100     :
+                       case    101     :
+                       case    102     :
+                       case    103     :
+                       case    104     :
+                       case    105     :
+                       case    106     :
+                       case    107     :
+                       case    108     :
+                       case    109     :
+                       case    110     :
+                       case    111     :
+                       case    112     :
+                       case    113     :
+                       case    114     :
+                       case    115     :
+                       case    116     :
+                       case    117     :
+                       case    118     :
+                       case    119     :
+                       case    120     :
+                       case    121     :
+                               *to = c;
+                               m_wrote_length = 1;
+                               break;
+                               
+                       case ' ':
+                               if ( _SpaceToPlus )
+                               {
+                                       *to = '+';
+                                       m_wrote_length = 1;
+                                       break;
+                               }
+                               
+                       default:
+                               to[0] = '%';
+                               /*char to1 = '0' + c / 16;
+                               if ( to1 > '9' )
+                                       to1 + 'A';
+                               to[1] = to1;
+                               to[2] = '0' + c % 16;
+                               if ( to[1] > '*/
+                               
+                               ROAST_CHAR_TO_HEXSTR(c,to+1,'A');
+                               
+                               m_wrote_length = 3;
+                               break;
+                       }
+               }
+               
+               size_t readed(){ return 1; }
+               size_t wrote(){ return m_wrote_length; }
+       };
+       //typedef ::roast::simple_encoder_base<url_encode_impl> url_encode;
+       
+       //////////////////////////////////////////////////////////////////////
+       
+       template <bool _SpaceToPlus=true>
+       class url_encode_ : public ::roast::simple_encoder_base<url_encode_impl<_SpaceToPlus>>
+       {
+       protected:
+               ::std::string m_str;
+       public:
+               url_encode_(){}
+               url_encode_(const char* from)
+               {
+                       size_t from_len = strlen(from);
+                       m_str.resize(from_len*3 + 1);
+                       
+                       encode(from, from_len, (char*)m_str.data());
+               }
+
+               const char* c_str(){ return m_str.c_str(); }
+               const char* get(){ return m_str.c_str(); }
+               const char* get_result(){ return m_str.c_str(); }
+               operator const char* (){ return m_str.c_str(); }
+       };
+       typedef url_encode_<> url_encode;
+}
+
+#endif//__SFJP_ROAST__roast__net__urlenc_HPP__