Browse Source

Added class UrlParser and logic

main
Jesus 2 years ago
parent
commit
363e5a6ddf
3 changed files with 74 additions and 1 deletions
  1. +15
    -0
      src/main/java/com/jesuspinar/webserver/Main.java
  2. +0
    -1
      src/main/java/com/jesuspinar/webserver/Manager.java
  3. +59
    -0
      src/main/java/com/jesuspinar/webserver/UrlParser.java

+ 15
- 0
src/main/java/com/jesuspinar/webserver/Main.java View File

@ -0,0 +1,15 @@
package com.jesuspinar.webserver;
public class Main{
public static void main(String[] args) {
UrlParser s = new UrlParser("http://www.jesuspinar.es/9000");
System.out.println("Url: "+s.getUrl());
System.out.println("Protocolo: "+s.getProtocol());
System.out.println("Dominio: "+s.getDomain());
System.out.println("Nombre: "+s.getDomainName());
System.out.println("TLD: "+s.getTld());
System.out.println("Puerto: "+s.getPort());
}
}

+ 0
- 1
src/main/java/com/jesuspinar/webserver/Manager.java View File

@ -52,7 +52,6 @@ public class Manager {
//Data from the server //Data from the server
model.addAttribute("url", currentURL); model.addAttribute("url", currentURL);
model.addAttribute("protocol", requestURL.getProtocol()); model.addAttribute("protocol", requestURL.getProtocol());
//ToDo: split host info in a class
model.addAttribute("host", requestURL.getHost()); model.addAttribute("host", requestURL.getHost());
model.addAttribute("port", requestURL.getPort()); model.addAttribute("port", requestURL.getPort());


+ 59
- 0
src/main/java/com/jesuspinar/webserver/UrlParser.java View File

@ -0,0 +1,59 @@
package com.jesuspinar.webserver;
public class UrlParser {
private String url;
private StringBuilder sb ;
public UrlParser(String url) {
sb = new StringBuilder();
this.url = url;
}
public String getUrl(){
return url;
}
private String splitString(String charStart, String charEnd , boolean reverse){
sb.setLength(0);
sb.append(url);
int start;
int end;
start = url.indexOf(charStart);
end = url.indexOf(charEnd);
if (reverse){
start = url.lastIndexOf(charStart)+1;
end = url.lastIndexOf(charEnd);
}
return sb.substring(start, end);
}
public String getProtocol(){
return splitString("h",":", false);
}
public String getDomain(){
int i = url.indexOf("/")+2;
return splitString(String.valueOf(url.charAt(i)),".", false);
}
public String getDomainName(){
sb.setLength(0);
sb.append(url);
int i = url.indexOf(".")+1;
sb.delete(0,i);
i = sb.toString().indexOf(".");
sb.delete(i,sb.length());
return sb.toString();
}
public String getTld(){
return splitString(".","/" , true);
}
public String getPort(){
return splitString("/","" , true);
}
}

Loading…
Cancel
Save