User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,375 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting

What do you use to parse the url?

Join Date: May 2007
Posts: 1
Reputation: Stan Vitvitskiy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Stan Vitvitskiy Stan Vitvitskiy is offline Offline
Newbie Poster

Re: What do you use to parse the url?

  #3  
May 17th, 2007
Well, try this function, it should be working.
  1. function parseURL(buffer) {
  2. var result = { };
  3. result.protocol = "";
  4. result.user = "";
  5. result.password = "";
  6. result.host = "";
  7. result.port = "";
  8. result.path = "";
  9. result.query = "";
  10.  
  11. var section = "PROTOCOL";
  12. var start = 0;
  13. var wasSlash = false;
  14.  
  15. while(start < buffer.length) {
  16. if(section == "PROTOCOL") {
  17. if(buffer.charAt(start) == ':') {
  18. section = "AFTER_PROTOCOL";
  19. start++;
  20. } else if(buffer.charAt(start) == '/' && result.protocol.length() == 0) {
  21. section = PATH;
  22. } else {
  23. result.protocol += buffer.charAt(start++);
  24. }
  25. } else if(section == "AFTER_PROTOCOL") {
  26. if(buffer.charAt(start) == '/') {
  27. if(!wasSlash) {
  28. wasSlash = true;
  29. } else {
  30. wasSlash = false;
  31. section = "USER";
  32. }
  33. start ++;
  34. } else {
  35. throw new ParseException("Protocol shell be separated with 2 slashes");
  36. }
  37. } else if(section == "USER") {
  38. if(buffer.charAt(start) == '/') {
  39. result.host = result.user;
  40. result.user = "";
  41. section = "PATH";
  42. } else if(buffer.charAt(start) == '?') {
  43. result.host = result.user;
  44. result.user = "";
  45. section = "QUERY";
  46. start++;
  47. } else if(buffer.charAt(start) == ':') {
  48. section = "PASSWORD";
  49. start++;
  50. } else if(buffer.charAt(start) == '@') {
  51. section = "HOST";
  52. start++;
  53. } else {
  54. result.user += buffer.charAt(start++);
  55. }
  56. } else if(section == "PASSWORD") {
  57. if(buffer.charAt(start) == '/') {
  58. result.host = result.user;
  59. result.port = result.password;
  60. result.user = "";
  61. result.password = "";
  62. section = "PATH";
  63. } else if(buffer.charAt(start) == '?') {
  64. result.host = result.user;
  65. result.port = result.password;
  66. result.user = "";
  67. result.password = "";
  68. section = "QUERY";
  69. start ++;
  70. } else if(buffer.charAt(start) == '@') {
  71. section = "HOST";
  72. start++;
  73. } else {
  74. result.password += buffer.charAt(start++);
  75. }
  76. } else if(section == "HOST") {
  77. if(buffer.charAt(start) == '/') {
  78. section = "PATH";
  79. } else if(buffer.charAt(start) == ':') {
  80. section = "PORT";
  81. start++;
  82. } else if(buffer.charAt(start) == '?') {
  83. section = "QUERY";
  84. start++;
  85. } else {
  86. result.host += buffer.charAt(start++);
  87. }
  88. } else if(section == "PORT") {
  89. if(buffer.charAt(start) = '/') {
  90. section = "PATH";
  91. } else if(buffer.charAt(start) == '?') {
  92. section = "QUERY";
  93. start++;
  94. } else {
  95. result.port += buffer.charAt(start++);
  96. }
  97. } else if(section == "PATH") {
  98. if(buffer.charAt(start) == '?') {
  99. section = "QUERY";
  100. start ++;
  101. } else {
  102. result.path += buffer.charAt(start++);
  103. }
  104. } else if(section == "QUERY") {
  105. result.query += buffer.charAt(start++);
  106. }
  107. }
  108.  
  109. if(section == "PROTOCOL") {
  110. result.host = result.protocol;
  111. result.protocol = "http";
  112. } else if(section == "AFTER_PROTOCOL") {
  113. throw new ParseException("Invalid url");
  114. } else if(section == "USER") {
  115. result.host = result.user;
  116. result.user = "";
  117. } else if(section == "PASSWORD") {
  118. result.host = result.user;
  119. result.port = result.password;
  120. result.user = "";
  121. result.password = "";
  122. }
  123.  
  124. return result;
  125. }
  126.  
  127. function ParseException(description) {
  128. this.description = description;
  129. }
Reply With Quote  
All times are GMT -4. The time now is 11:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC