-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebFileCrawler.go
295 lines (272 loc) · 8.89 KB
/
WebFileCrawler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"FileCrawler/search"
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
//Page holds Title and Greeting
// type Page struct {
// Title string
// Greeting string
// Body string
// SearchTerm string
// FoundCount int
// StartLoc string
// }
// func loadStartPage(pageName string, title string) (*Page, error) {
// greeting := "Welcome to the Web version of FileCrawler! Search the contents of files for your search term or regular expression..."
// return &Page{Title: title, Greeting: greeting}, nil
// }
// func loadSearchPage(pageName string, title string, term string, body string, foundcount int, startloc string) (*Page, error) {
// greeting := "The results are in!"
// return &Page{Title: title, Greeting: greeting, SearchTerm: term, Body: body,
// FoundCount: foundcount, StartLoc: startloc}, nil
// }
// func renderTemplate(w http.ResponseWriter, tmpl string, p *Page, templates *template.Template) {
// err := templates.ExecuteTemplate(w, tmpl+".html", p)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// }
// }
func startHandler(w http.ResponseWriter, r *http.Request, templates *template.Template) {
// p, err := loadStartPage("start", "WebFileCrawler")
// if err != nil {
// http.Redirect(w, r, "/", http.StatusFound)
// return
// }
// renderTemplate(w, "start", p, templates)
body := `<head>
<script>
function validate(){
msg.textContent = ""
event.returnValue = true;
if (document.getElementById("myloc").value == ""){
msg.textContent += "You need a starting location. ";
event.returnValue = false;
}
if (document.getElementById("myterm").value == "" && document.getElementById("myreg").value == ""){
msg.textContent += "You need a search term or regular expression. ";
event.returnValue = false;
}
var reggie = /^[0-9]{1,4}$/;
var mymaxval = document.getElementById("mymax").value;
var mymaxvalerrmsg = "Max found files must be blank or a number from 1 to 9999. ";
if (mymaxval != "" && !reggie.test(mymaxval) ){
msg.textContent += mymaxvalerrmsg;
event.returnValue = false;
}
else {
if (mymaxval =="0"){
msg.textContent += mymaxvalerrmsg;
event.returnValue = false;
}
}
return event.returnValue
}
</script>
</head>
<h1>start</h1>
<div><pre>Welcome to the Web version of FileCrawler! Search the contents of files for your search term or regular expression...</pre></div>
<style>
label{
text-align:right;
}
input {
display:inline-block;
margin-left:4px
}
</style>
<form id="myform" action="/search" method="POST" onsubmit="event.preventDefault(); validate();">
<div>
<label for="myloc">Starting Folder:</label>
<input type="text" id="myloc" name="myloc"/>
</div>
<br/>
<div>
<label for="mytypes">File Types (e.g. txt,doc.) Default is txt:</label>
<input type="text" id="mytypes" name="mytypes"/>
</div>
<br/>
<div>
<label for="myterm">Search For:</label>
<input type="text" id="myterm" name="myterm"/>
</div>
<br/>
<div>
<label for="mycase">Case sensitive:</label>
<input type="checkbox" id="mycase" name="mycase"/>
</div>
<br/>
<div>
<label for="mylog">Log file (optional):</label>
<input type="text" id="mylog" name="mylog"/>
<div>
<br/>
<div>
<label for="myreg">Regular expression (supercedes search term):</label>
<input type="text" id="myreg" name="myreg"/>
<div>
<br/>
<div>
<label for="mymax">Max found files limit. Default is 250:</label>
<input type="text" id="mymax" name="mymax"/>
<div>
<br/>
<div>
<input type="submit" value="Submit" style="position:absolute;left:10px">
</div>
<br/>
<br/>
<div>
<label id="msg" style="color:red;position:absolute;left:10px" />
</div>
</form>`
w.Write([]byte(body))
}
func writeSearchPageResponse(w http.ResponseWriter, mytitle string, foundcount int, searchterm string,
root string, foundfiles string) {
body := `<h1>` + mytitle + `</h1>
<div><pre>The results are in! ` + strconv.Itoa(foundcount) + ` file(s) matched your search for "` + searchterm + `" in ` + root + `.</pre></div>
<body>
<div><pre>` +
foundfiles +
`</pre></div>
</body>`
w.Write([]byte(body))
}
func searchHandler(w http.ResponseWriter, r *http.Request, templates *template.Template) {
foundfiles := ""
foundcount := 0
var searchterm = "UNKNOWN"
mytitle := "Search Results"
var searchfunc func(string, os.FileInfo, error) error
var echoback []string
foundlimit := 250
var err error
var reg *regexp.Regexp
//default is case-insensitive
caseterm := "(?i)"
var caseflag string
var root string
logname := "echoback"
var extensions []string
var regexpterm string
var maxval string
if err = r.ParseForm(); err != nil {
foundfiles = err.Error()
mytitle = "Error parsing request form"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
searchterm = r.Form.Get("myterm")
regexpterm = r.Form.Get("myreg")
root = r.Form.Get("myloc")
maxval = r.Form.Get("mymax")
if maxval != "" {
if foundlimit, err = strconv.Atoi(maxval); err != nil {
foundfiles = err.Error()
mytitle = "Error parsing max files found limit"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
}
extensions = strings.Split(r.Form.Get("mytypes"), ",")
if len(extensions) == 0 || (len(extensions) == 1 && extensions[0] == "") {
extensions = []string{"txt"}
}
if regexpterm != "" {
if reg, err = regexp.Compile(regexpterm); err != nil {
foundfiles = err.Error()
mytitle = "Error compiling search term"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
searchterm = regexpterm
} else {
caseflag = r.Form.Get("mycase")
if caseflag == "on" {
caseterm = ""
}
if reg, err = regexp.Compile(caseterm + searchterm); err != nil {
foundfiles = err.Error()
mytitle = "Error compiling search term"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
}
echoback = make([]string, foundlimit, foundlimit)
for i := range echoback {
echoback[i] = "!"
}
if searchfunc, err = search.Factory(extensions, reg, logname, foundlimit,
&foundcount, echoback); err != nil {
foundfiles = err.Error()
mytitle = "Error creating search function"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
if err = filepath.Walk(root, searchfunc); err != nil {
foundfiles = err.Error()
mytitle = "Error walking filepath"
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
return
}
for _, foundfile := range echoback {
if foundfile == "!" {
break
}
foundfiles += fmt.Sprintf("%s%s", foundfile, "\n")
}
writeSearchPageResponse(w, mytitle, foundcount, searchterm, root, foundfiles)
// var p *Page
// if p, err = loadSearchPage("search", mytitle, searchterm, foundfiles, foundcount, root); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// }
// renderTemplate(w, "search", p, templates)
// body := `<h1>` + mytitle + `</h1>
// <div><pre>The results are in!` + strconv.Itoa(foundcount) + ` file(s) matched your search for "` + searchterm + `" in ` + root + `.</pre></div>
// <body>
// <div><pre>` +
// foundfiles +
// `</pre></div>
// </body>`
// w.Write([]byte(body))
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, *template.Template), validPath *regexp.Regexp,
templates *template.Template) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, templates)
}
}
func main() {
portptr := flag.String("port", "", "The port for localhost to listen on. Example: -post=8099")
flag.Parse()
port := *portptr
if port == "" {
fmt.Println("Port is not set. Example: WebFileCrawler -port=8099")
os.Exit(1)
}
validPath := regexp.MustCompile("(/)|(/search)")
templates := template.Must(template.ParseFiles("start.html", "search.html"))
http.HandleFunc("/", makeHandler(startHandler, validPath, templates))
http.HandleFunc("/search", makeHandler(searchHandler, validPath, templates))
cmd := exec.Command("rundll32", "url.dll,FileProtocolHandler", fmt.Sprintf("http://localhost:%v/", port))
if err := cmd.Start(); err != nil {
fmt.Println(fmt.Sprintf("Problem launching default browser: %v", err.Error()))
os.Exit(1)
}
http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
}