PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/gnu-cpp/core/src/wsb-core/common/URI.cpp

https://bitbucket.org/osi/wsb-core
C++ | 209 lines | 105 code | 58 blank | 46 comment | 10 complexity | 9b140c35dc22320c848dcc34a19af66d MD5 | raw file
  1. /**
  2. * URI.cpp
  3. *
  4. * This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
  5. * To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
  6. * a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
  7. *
  8. */
  9. // Includes
  10. //-----------------
  11. //-- Std
  12. #include <string>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <malloc.h>
  16. using namespace std;
  17. //-- Regexp
  18. #include <re_comp.h>
  19. #include <regex.h>
  20. #include "URI.h"
  21. namespace OSI {
  22. namespace WSB {
  23. URI::URI(string uri) {
  24. //-- Default to parsing results
  25. this->valid = false;
  26. this->absolute = false;
  27. //-- Record URI
  28. this->uri = uri;
  29. //-- Prepare URI Regexp
  30. char expr[2048];
  31. URI_CREATEREGEXP(expr)
  32. int errcode = regcomp(&(this->regexp),expr,REG_EXTENDED);
  33. if (errcode!=0) {
  34. size_t length = regerror (errcode, &(this->regexp), NULL, 0);
  35. char *buffer = (char*) calloc (length,sizeof(char));
  36. (void) regerror (errcode, &(this->regexp), buffer, length);
  37. cerr << "Error while compiling: "<< endl;
  38. cerr << "\t Expression: " << expr<< endl;
  39. cerr << "\t Result: " << buffer<< endl;
  40. }
  41. //-- Test
  42. size_t nmatch = 20;
  43. regmatch_t matchptr[nmatch];
  44. errcode = regexec(&(this->regexp),this->uri.c_str(),nmatch,matchptr,0);
  45. if (errcode!=0) {
  46. size_t length = regerror (errcode, &(this->regexp), NULL, 0);
  47. char *buffer = (char*) calloc (length,sizeof(char));
  48. (void) regerror (errcode, &(this->regexp), buffer, length);
  49. cerr << "Error while matching: " << endl;
  50. cerr << "\t Source: " << this->uri<< endl;
  51. cerr << "\t Expression: " << expr << endl;
  52. cerr << "\t Result: " << buffer<< endl;
  53. }
  54. //-- Extract the scheme
  55. this->valid = true;
  56. this->scheme = this->uri.substr(matchptr[1].rm_so,matchptr[1].rm_eo-matchptr[1].rm_so);
  57. //cout << "-Scheme: " << this->scheme << endl;
  58. // Continue analysis
  59. //-------------------------------
  60. //-- If the next group is valid we have an authority
  61. int np = 1;
  62. np++;
  63. string next;
  64. if (matchptr[np].rm_so!=-1) {
  65. //-- Get the authority
  66. next = this->uri.substr(matchptr[np].rm_so,matchptr[np].rm_eo-matchptr[np].rm_so);
  67. string authorityContent = next.length()>2 ? next.substr(2,next.length()-2):"";
  68. //cout << "Authority: " << authorityContent << endl;
  69. //-- Is absolute then
  70. this->absolute = true;
  71. if (authorityContent.length()>0) {
  72. //-- Look for host in authority
  73. //--------------------------------------
  74. //-- Look and separate parts (the next subexp is the entire authority content)
  75. np++;
  76. np++;
  77. //-- The first group could be the user
  78. if (matchptr[np].rm_so!=-1) {
  79. next = this->uri.substr(matchptr[np].rm_so,matchptr[np].rm_eo-matchptr[np].rm_so);
  80. this->username = next.substr(0,next.length()-1);
  81. //cout << "\t\tHost user: " << this->username << endl;
  82. }
  83. //-- Next is the host
  84. np++;
  85. this->host = this->uri.substr(matchptr[np].rm_so,matchptr[np].rm_eo-matchptr[np].rm_so);
  86. np++; // Jump over inside host group
  87. //cout << "\t\tHost:" << this->host << endl;
  88. //-- Next could be port
  89. np++;
  90. if (matchptr[np].rm_so!=-1) {
  91. // Remove ':' character in front of port
  92. next = this->uri.substr(matchptr[np].rm_so,matchptr[np].rm_eo-matchptr[np].rm_so);
  93. next = next.substr(1,next.length()-1);
  94. this->port = atoi(next.c_str());
  95. //cout << "\t\tPort: " << this->port << endl;
  96. } else {
  97. this->port = 0;
  98. }
  99. } else {
  100. // Jump over all non matched groups in authority
  101. np+=5;
  102. } // EOF if authority content
  103. } else {
  104. // Jump over all non matched groups in authority
  105. np+=5;
  106. }
  107. //-- After that, there coud be a path
  108. np++;
  109. //cout << "Looking for path at np: " << np << endl;
  110. if (matchptr[np].rm_so!=-1) {
  111. next = this->uri.substr(matchptr[np].rm_so,matchptr[np].rm_eo-matchptr[np].rm_so);
  112. cout << "Path: " << next << endl;
  113. }
  114. // for (int i= 0; i<nmatch;i++) {
  115. // if (matchptr[i].rm_so!=-1) {
  116. //
  117. // // cout << "Match["<< i <<"]: "<< this->uri.substr(matchptr[i].rm_so,matchptr[i].rm_eo-matchptr[i].rm_so) << endl;
  118. //
  119. // }
  120. // }
  121. regfree(&(this->regexp));
  122. }
  123. URI::~URI() {
  124. // TODO Auto-generated destructor stub
  125. }
  126. string URI::getURI() {
  127. return this->uri;
  128. }
  129. bool URI::isValid() {
  130. return this->valid;
  131. }
  132. string URI::getScheme() {
  133. return this->scheme;
  134. }
  135. bool URI::isAbsolute() {
  136. return this->absolute;
  137. }
  138. string URI::getHost() {
  139. return this->host;
  140. }
  141. string URI::getUserName() {
  142. return this->username;
  143. }
  144. int URI::getPort() {
  145. return this->port;
  146. }
  147. string URI::getPath() {
  148. return this->path;
  149. }
  150. } /* namespace WSB */
  151. } /* namespace OSI */