this one burned up on re-entry, but philly was great!
[k_transpose.git] / httpd.go
index d50afdcd50f23770a924dc834dba48b70ca94a18..29b6ca9d36e37f4d66cdda3ee3b6a62ba6987011 100644 (file)
--- a/httpd.go
+++ b/httpd.go
@@ -4,11 +4,15 @@ import(
     "fmt"
     "os"
 
+    "io"
+    "io/ioutil"
+    "bufio"
+
     "strings"
     "strconv"
-    "bufio"
 
     "net/http"
+       "net/url"
 
     "github.com/wsxiaoys/terminal/color"
 )
@@ -169,8 +173,12 @@ func parseColors(colorfile *os.File) (pal ktPalette, err error) {
     return ret, e.err
 }
 
+var httpdStatus bool
+
 func ktInit(dirPrepend string, port int, colorfilePath string) error {
 
+    httpdStatus = false
+
     color.Print("@yparsing colorfile :: @{|}")
     file, err := os.Open(colorfilePath)
     if err != nil {
@@ -189,19 +197,160 @@ func ktInit(dirPrepend string, port int, colorfilePath string) error {
 
     if pal == ansiColors {}
 
+    color.Printf("@yverifying & preprocessing colorsets@{|} :: @y[SKIP]\n")
+    color.Printf("@ygenerating transpositional colorspace@{|} :: @y[SKIP]\n")
+
     color.Printf("@ystarting httpd on port @b%d@{|} :: ", port)
+    http.HandleFunc("/kt/", transposePage)
+
+    var portString string
+    fmt.Sprintf(portString, ":%d", port)
+    err = http.ListenAndServe(portString, nil)
+    if err != nil {
 
+        color.Printf("@r[%s]@{-} - run me as root!\n", xM)
+        return fmt.Errorf("%s\n", "failed to start httpd")
+    }
+
+    color.Printf("@g[%s]@{-}\n", checkM)
 
     return nil
 }
 
+func transposePage(writer http.ResponseWriter, req *http.Request) {
+
+    if !httpdStatus {
+
+        httpdStatus = true
+        color.Printf("@g[%s]@{|}\n", checkM)
+    }
+
+    if req.URL.Path == "/kt/" {
+        writer.Write([]byte("wtf"))
+       return
+    }
+
+    fqdn      := req.URL.Path[4:]
+    targetURL := fmt.Sprintf("http://%s", fqdn)
+
+    resp, err := http.Get(targetURL)
+
+    if err != nil || resp.StatusCode != 200 {
+
+        io.WriteString(writer, "failed to get that page! -kt\n")
+        io.WriteString(writer, targetURL + "\n")
+
+        io.WriteString(writer, resp.Status)
+        return
+    }
+
+       conType := resp.Header.Get("Content-Type")
+
+    switch conType[0:strings.Index(conType, ";")] {
+
+        case "text/html":
+            writer.Write(transposeHTML(bufio.NewScanner(resp.Body), fqdn))
+
+        case "text/css":
+            writer.Write(transposeCSS(bufio.NewScanner(resp.Body), fqdn))
+
+        default:
+            page, _ := ioutil.ReadAll(resp.Body)
+            writer.Write(page)
+    }
+
+    resp.Body.Close()
+}
+
+/* swap href="" & src="" */
+func transposeHTML(scan *bufio.Scanner, fqdn string) []byte {
+
+    var ret []byte
+       var i int
+
+       scan.Split(bufio.ScanWords)
+       for scan.Scan() {
+
+               i++
+               cur := scan.Text()
+
+               //fmt.Printf("%s\n", cur)
+
+               if len(cur) < 7 {
+
+
+               } else if(cur[0:6] == "href=\\") {
+
+                       urlStr := cur[7:strings.Index(cur[7:], "\\") + 7]
+
+                       u, err := url.Parse(urlStr)
+                       if err != nil {
+                               fmt.Printf("malformed URL: %s\n", urlStr)
+                       }
+
+                       if u.Host == "" {
+
+                               u.Host = fmt.Sprintf("localhost/kt/%s", fqdn)
+//                             cur = append(cur[0:6], 
+                       }
+
+                       fmt.Printf("[F] URL: %s // PATH: %s\n", u.Host, u.Path)
+                       if u == u {}
+
+
+               } else if(cur[0:5] == "href=") {
+
+                       urlStr := cur[6:strings.Index(cur[6:], "\"") + 6]
+
+                       u, err := url.Parse(urlStr)
+                       if err != nil {
+                               fmt.Printf("malformed URL: %s\n", urlStr)
+                       }
+
+                       if u == u {}
+                       fmt.Printf("URL: %s // PATH: %s\n", u.Host, u.Path)
+
+               } else if(cur[0:5] == "src=\"") {
+
+                       //fmt.Printf("%s\n", cur)
+                       urlStr := cur[5:strings.Index(cur[5:], "\"") + 5]
+
+                       u, err := url.Parse(urlStr)
+                       if err != nil {
+                               fmt.Printf("malformed URL: %s\n", urlStr)
+                       }
+
+                       if u.Host == "" {
+
+                               u.Host = fmt.Sprintf("localhost/kt/%s", fqdn)
+//                             cur = append(cur[0:6], 
+
+                       fmt.Printf("[S] URL: %s // PATH: %s\n", u.Host, u.Path)
+                       if u == u {}
+                       }
+
+               }
+
+               ret = append(ret, byte(' '))
+               ret = append(ret, cur...)
+       }
+
+       fmt.Printf("%d\n", i)
+
+    return ret
+}
+
+func transposeCSS(scan *bufio.Scanner, fqdn string) []byte {
+
+    var ret []byte
+
+    return ret
+}
+
 func main() {
 
     err := ktInit("kolors", 999, "/home/kremlin/.Xresources")
 
-    /* make sure to close() anything you need to (you need to) */
-    _, err = http.Get("http://kremlin.cc")
-
     if err != nil {}
 }