// Path: androidnetspoof/src/main/java/uk/digitalsquid/netspoofer/proxy/HttpRequest.java
// public class HttpRequest extends HttpMessage {
//     private String method;
//     private String path;
//     private String version;
// 
//     public boolean setRequestLine(String line) {
//         StringTokenizer tkz = new StringTokenizer(line, " ");
//         if(!tkz.hasMoreTokens()) return false;
//         method = tkz.nextToken();
//         if(!tkz.hasMoreTokens()) return false;
//         path = tkz.nextToken();
//         if(!tkz.hasMoreTokens()) return false;
//         version = tkz.nextToken();
//         return true;
//     }
//     public String getRequestLine() {
//         return String.format("%s %s %s", method, path, version);
//     }
//     
//     public String getMethod() { return method; }
//     public void setMethod(String method) { this.method = method; }
//     public String getPath() { return path; }
//     public void setPath(String path) { this.path = path; }
//     public String getVersion() { return version; }
//     public void setVersion(String version) { this.version = version; }
//     
//     @Override
//     public String toString() {
//         return String.format("%s\r\n%s",
//                 getRequestLine(),
//                 getHeaders());
//                 
//     }
//     public Uri getUri() {
//         return Uri.parse(String.format("http://%s/%s", getHost(), getPath()));
//     }
//     public void setUri(Uri uri) {
//         setHost(uri.getHost());
//         // TODO: Check this is correct
//         setPath(uri.getEncodedPath());
//     }
//     
//     private boolean ignoreResponse = false;
//     
//     /**
//      * If this method is called at any point then an empty response will be
//      * returned; no request is actually made. This allows a spoof to completely
//      * rewrite a response with less overhead.
//      */
//     public void returnNullResponse() {
//         ignoreResponse = true;
//     }
//     /**
//      * If <code>true</code> indicates that no request should be made; a blank 
//      * response should be returned.
//      */
//     public boolean shouldIgnoreResponse() {
//         return ignoreResponse;
//     }
//     
//     @Override
//     public void reset() {
//         super.reset();
//         method = "";
//         path = "";
//         version = "";
//         ignoreResponse = false;
//     }
// }
// 
// Path: androidnetspoof/src/main/java/uk/digitalsquid/netspoofer/proxy/HttpResponse.java
// public class HttpResponse extends HttpMessage {
//     private int responseCode;
//     private String responseMessage;
//     public String getResponseMessage() {
//         return responseMessage;
//     }
//     public void setResponseMessage(String responseMessage) {
//         this.responseMessage = responseMessage;
//     }
//     public int getResponseCode() {
//         return responseCode;
//     }
//     public void setResponseCode(int responseCode) {
//         this.responseCode = responseCode;
//     }
// 
//     // A few functions for getting certain headers
//     public String getContentType() {
//         List<String> result = headers.get("content-type");
//         if(result == null) return "";
//         return result.get(0);
//     }
//     
//     @Override
//     public void reset() {
//         super.reset();
//         responseCode = 0;
//         responseMessage = "";
//     }
// }


import uk.digitalsquid.netspoofer.proxy.HttpRequest;
import uk.digitalsquid.netspoofer.proxy.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/*
 * This file is part of Network Spoofer for Android.
 * Network Spoofer lets you change websites on other people’s computers
 * from an Android phone.
 * Copyright (C) 2014 Will Shackleton <will@digitalsquid.co.uk>
 *
 * Network Spoofer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Network Spoofer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Network Spoofer, in the file COPYING.
 * If not, see <http://www.gnu.org/licenses/>.
 */

package uk.digitalsquid.netspoofer.spoofs;




/**
 * Represents a spoof that manipulates the HTML content of a web page.
 * @author Will Shackleton <w.shackleton@gmail.com>
 *
 */
public class HtmlSpoof extends Spoof {

    private static final long serialVersionUID = -1966412296143206193L;
    
    private List<HtmlEditorSpoof> editors = new ArrayList<HtmlEditorSpoof>();

    public HtmlSpoof() {
        super("(Internal HTML transformer)", "all HTML transformations grouped together");
    }
    public HtmlSpoof(HtmlEditorSpoof s) {
        this();
        addEditor(s);
    }

    @Override
    public void modifyRequest(HttpRequest request) {
    }

    @Override
    public void modifyResponse(HttpResponse response, HttpRequest request) {
        List<String> contentType = response.getHeader("Content-Type");
        if(contentType == null) return;
        boolean isHtml = false;
        for(String type : contentType) {
            if(type.toLowerCase(Locale.ENGLISH).startsWith("text/html"))
                isHtml = true;
        }
        if(!isHtml) return;
        
        Document doc = Jsoup.parse(new String(response.getContent()));
// Path: androidnetspoof/src/main/java/uk/digitalsquid/netspoofer/proxy/HttpRequest.java
// public class HttpRequest extends HttpMessage {
//     private String method;
//     private String path;
//     private String version;
// 
//     public boolean setRequestLine(String line) {
//         StringTokenizer tkz = new StringTokenizer(line, " ");
//         if(!tkz.hasMoreTokens()) return false;
//         method = tkz.nextToken();
//         if(!tkz.hasMoreTokens()) return false;
//         path = tkz.nextToken();
//         if(!tkz.hasMoreTokens()) return false;
//         version = tkz.nextToken();
//         return true;
//     }
//     public String getRequestLine() {
//         return String.format("%s %s %s", method, path, version);
//     }
//     
//     public String getMethod() { return method; }
//     public void setMethod(String method) { this.method = method; }
//     public String getPath() { return path; }
//     public void setPath(String path) { this.path = path; }
//     public String getVersion() { return version; }
//     public void setVersion(String version) { this.version = version; }
//     
//     @Override
//     public String toString() {
//         return String.format("%s\r\n%s",
//                 getRequestLine(),
//                 getHeaders());
//                 
//     }
//     public Uri getUri() {
//         return Uri.parse(String.format("http://%s/%s", getHost(), getPath()));
//     }
//     public void setUri(Uri uri) {
//         setHost(uri.getHost());
//         // TODO: Check this is correct
//         setPath(uri.getEncodedPath());
//     }
//     
//     private boolean ignoreResponse = false;
//     
//     /**
//      * If this method is called at any point then an empty response will be
//      * returned; no request is actually made. This allows a spoof to completely
//      * rewrite a response with less overhead.
//      */
//     public void returnNullResponse() {
//         ignoreResponse = true;
//     }
//     /**
//      * If <code>true</code> indicates that no request should be made; a blank 
//      * response should be returned.
//      */
//     public boolean shouldIgnoreResponse() {
//         return ignoreResponse;
//     }
//     
//     @Override
//     public void reset() {
//         super.reset();
//         method = "";
//         path = "";
//         version = "";
//         ignoreResponse = false;
//     }
// }
// 
// Path: androidnetspoof/src/main/java/uk/digitalsquid/netspoofer/proxy/HttpResponse.java
// public class HttpResponse extends HttpMessage {
//     private int responseCode;
//     private String responseMessage;
//     public String getResponseMessage() {
//         return responseMessage;
//     }
//     public void setResponseMessage(String responseMessage) {
//         this.responseMessage = responseMessage;
//     }
//     public int getResponseCode() {
//         return responseCode;
//     }
//     public void setResponseCode(int responseCode) {
//         this.responseCode = responseCode;
//     }
// 
//     // A few functions for getting certain headers
//     public String getContentType() {
//         List<String> result = headers.get("content-type");
//         if(result == null) return "";
//         return result.get(0);
//     }
//     
//     @Override
//     public void reset() {
//         super.reset();
//         responseCode = 0;
//         responseMessage = "";
//     }
// }


// Path: androidnetspoof/src/main/java/uk/digitalsquid/netspoofer/spoofs/HtmlSpoof.java
import uk.digitalsquid.netspoofer.proxy.HttpRequest;
import uk.digitalsquid.netspoofer.proxy.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/*
 * This file is part of Network Spoofer for Android.
 * Network Spoofer lets you change websites on other people’s computers
 * from an Android phone.
 * Copyright (C) 2014 Will Shackleton <will@digitalsquid.co.uk>
 *
 * Network Spoofer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Network Spoofer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Network Spoofer, in the file COPYING.
 * If not, see <http://www.gnu.org/licenses/>.
 */

package uk.digitalsquid.netspoofer.spoofs;




/**
 * Represents a spoof that manipulates the HTML content of a web page.
 * @author Will Shackleton <w.shackleton@gmail.com>
 *
 */
public class HtmlSpoof extends Spoof {

    private static final long serialVersionUID = -1966412296143206193L;
    
    private List<HtmlEditorSpoof> editors = new ArrayList<HtmlEditorSpoof>();

    public HtmlSpoof() {
        super("(Internal HTML transformer)", "all HTML transformations grouped together");
    }
    public HtmlSpoof(HtmlEditorSpoof s) {
        this();
        addEditor(s);
    }

    @Override
    public void modifyRequest(HttpRequest request) {
    }

    @Override
    public void modifyResponse(HttpResponse response, HttpRequest request) {
        List<String> contentType = response.getHeader("Content-Type");
        if(contentType == null) return;
        boolean isHtml = false;
        for(String type : contentType) {
            if(type.toLowerCase(Locale.ENGLISH).startsWith("text/html"))
                isHtml = true;
        }
        if(!isHtml) return;
        
        Document doc = Jsoup.parse(new String(response.getContent()));
        Elements bodys = doc.select("body");