Describe Features/StoreID/Helper/Golang here.

   1 package main
   2 /*
   3 license note
   4 Copyright (c) 2015, Eliezer Croitoru
   5 All rights reserved.
   6 
   7 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
   8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
   9 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  11 
  12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13 */
  14 import (
  15         "bufio"
  16         "flag"
  17         "fmt"
  18         "os"
  19         "regexp"
  20         "strings"
  21 )
  22 
  23 var debug *string
  24 var answer *string
  25 var path *string
  26 
  27 var err error
  28 var regex [256]*regexp.Regexp
  29 
  30 func process_request(line string, re [256]*regexp.Regexp) {
  31 
  32         lparts := strings.Split(strings.TrimRight(line, "\n"), " ")
  33         if len(lparts[0]) > 0 {
  34                 if *debug == "yes" {
  35                         fmt.Fprintln(os.Stderr, "ERRlog: Proccessing request => \""+strings.TrimRight(line, "\n")+"\"")
  36                 }
  37         }
  38                 res := re[0].FindAllStringSubmatch(lparts[1] ,-1)
  39                 if res != nil {
  40                                 fmt.Println("A Match have been found:")
  41                                 fmt.Println(res)
  42                                 fmt.Println(len(res))
  43                                 fmt.Println("===== and now the result")
  44                                 fmt.Println(lparts[0] + " OK store-id=" + "http://dl.sourceforge.net.squid.internal/" + res[0][1] )
  45                                 return
  46                 }
  47 
  48                 res = re[1].FindAllStringSubmatch(lparts[1] ,-1)
  49                 if len(res) > 0 {
  50                                 fmt.Println("A Match have been found:")
  51                                 fmt.Println(res)
  52                                 fmt.Println(len(res))
  53                                 fmt.Println("===== and now the result")
  54                                 fmt.Println(lparts[0] + " OK store-id=" + "http://ytimg.squid.internal/" + res[0][1] )
  55                                 return
  56                 }
  57 
  58                 res = re[2].FindAllStringSubmatch(lparts[1] ,-1)
  59                 if len(res) > 0 {
  60                                 fmt.Println("A Match have been found:")
  61                                 fmt.Println(res)
  62                                 fmt.Println(len(res))
  63                                 fmt.Println("===== and now the result")
  64                                 fmt.Println(lparts[0] + " OK store-id=" + "http://vimeo-video.squid.internal/" + res[0][2] )
  65                                 return
  66                 }
  67 
  68 
  69         fmt.Println(lparts[0] + " " + *answer)
  70 }
  71 
  72 func main() {
  73         fmt.Fprintln(os.Stderr, "ERRlog: Starting Fake helper")
  74 
  75         debug = flag.String("d", "no", "Debug mode can be \"yes\" or something else for no")
  76         answer = flag.String("a", "ERR", "Answer can be either \"ERR\" or \"OK\"")
  77         path = flag.String("p", "patterns.txt", "a full or relative path for patterns file")
  78         flag.Parse()
  79 //Source forge pattern: ^http:\/\/[^\.]+\.dl\.sourceforge\.net\/(.*)                    http://dl.sourceforge.net.squid.internal/$1
  80 
  81         regex[0] = regexp.MustCompile("^https?:\\/\\/[^\\.]+\\.dl\\.sourceforge\\.net\\/(.*)")
  82         regex[1] = regexp.MustCompile("^https?:\\/\\/.*\\.ytimg.com\\/(.*\\.jpg|.*\\.gif|.*\\.js)")
  83         regex[2] = regexp.MustCompile("^^https?:\\/\\/(pdlvimeocdn-a|avvimeo-a)\\.akamaihd\\.net\\/([\\w\\d\\-\\_\\.\\/]+\\.mp4)?")
  84 
  85         reader := bufio.NewReader(os.Stdin)
  86 
  87         for {
  88                 line, err := reader.ReadString('\n')
  89 
  90                 if err != nil {
  91                         // You may check here if err == io.EOF
  92                         break
  93                 }
  94                 if strings.HasPrefix(line, "q") || strings.HasPrefix(line, "Q") {
  95                         fmt.Fprintln(os.Stderr, "ERRlog: Exiting cleanly")
  96                         break
  97                 }
  98 
  99                 go process_request(line, regex)
 100 
 101         }
 102 }

Features/StoreID/Helper/Golang (last edited 2015-05-24 18:39:45 by Eliezer Croitoru)