-
Notifications
You must be signed in to change notification settings - Fork 10
/
anscdn.go
424 lines (345 loc) · 9.28 KB
/
anscdn.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
*
* AnsCDN Copyright (C) 2010 Robin Syihab (r [at] nosql.asia)
* Simple CDN server written in Golang.
*
* License: General Public License v2 (GPLv2)
*
* Copyright (c) 2009 The Go Authors. All rights reserved.
*
**/
package main;
import (
"strings"
"strconv"
"fmt"
"http"
"os"
"path"
"mime"
"utf8"
"flag"
"./anlog"
"./filemon"
"./config"
"./cdnize"
"./downloader"
)
const (
VERSION = "0.14"
)
var cfg *config.AnscdnConf
var quiet bool
func file_exists(file_path string) bool{
file, err := os.Open(file_path)
if err != nil {
return false
}
file.Close()
return true
}
// Heuristic: b is text if it is valid UTF-8 and doesn't
// contain any unprintable ASCII or Unicode characters.
func isText(b []byte) bool {
for len(b) > 0 && utf8.FullRune(b) {
rune, size := utf8.DecodeRune(b)
if size == 1 && rune == utf8.RuneError {
// decoding error
return false
}
if 0x80 <= rune && rune <= 0x9F {
return false
}
if rune < ' ' {
switch rune {
case '\n', '\r', '\t':
// okay
default:
// binary garbage
return false
}
}
b = b[size:]
}
return true
}
func setHeaderCond(con http.ResponseWriter, abs_path string, data []byte) {
extension := path.Ext(abs_path)
if ctype := mime.TypeByExtension(extension); ctype != "" {
con.Header().Set("Content-Type", ctype)
}else{
if isText(data) {
con.Header().Set("Content-Type", "text-plain; charset=utf-8")
} else {
con.Header().Set("Content-Type", "application/octet-stream") // generic binary
}
}
}
func validUrlPath(url_path string) bool{
return strings.Index(url_path,"../") < 1
}
func write(c http.ResponseWriter, f string, v ...interface{}){fmt.Fprintf(c,f,v...);}
func MainHandler(con http.ResponseWriter, r *http.Request){
url_path := r.URL.Path[1:]
if len(url_path) == 0{
http.Error(con,"404",http.StatusNotFound)
return
}
// security check
if !validUrlPath(url_path){
write(con,"Invalid url path")
anlog.Warn("Invalid url_path: %s\n",url_path)
return
}
if len(cfg.UrlMap) > 1 && strings.HasPrefix(url_path,cfg.UrlMap[1:]) == true{
url_path = url_path[len(cfg.UrlMap):]
}
// restrict no ext
if cfg.IgnoreNoExt && len(path.Ext(url_path)) == 0 {
anlog.Warn("Ignoring `%s`\n", url_path)
http.Error(con, "404", http.StatusNotFound)
return
}
// restrict ext
if len(cfg.IgnoreExt) > 0 {
cext := path.Ext(url_path)
if len(cext) > 1{
cext = strings.ToLower(cext[1:])
exts := strings.Split(cfg.IgnoreExt,",")
for _, ext := range exts{
if cext == strings.Trim(ext," ") {
anlog.Warn("Ignoring `%s` by extension.\n", url_path)
http.Error(con, "404", http.StatusNotFound)
return
}
}
}
}
var abs_path string
if strings.HasPrefix(cfg.StoreDir,"./"){
abs_path, _ = os.Getwd()
abs_path = path.Join(abs_path, cfg.StoreDir[1:], url_path)
}else{
abs_path = path.Join(cfg.StoreDir,url_path)
}
dir_name, _ := path.Split(abs_path)
if !file_exists(abs_path) {
url_source := "http://" + cfg.BaseServer + "/" + url_path
err := os.MkdirAll(dir_name,0755)
if err != nil {
fmt.Fprintf(con,"404 Not found (e)")
anlog.Error("Cannot MkdirAll. error: %s\n",err.String())
return
}
// download it
var data []byte
rv, lm, total_size := downloader.Download(url_source, abs_path, cfg.Strict, &data)
if rv == false{
fmt.Fprintf(con, "404 Not found")
return
}
// send to client for the first time.
setHeaderCond(con, abs_path, data)
// set Last-modified header
con.Header().Set("Last-Modified", lm)
for {
bw, err := con.Write(data)
if err != nil || bw == 0 {
break
}
if bw >= total_size {
break
}
}
}else{
if cfg.CacheOnly {
// no static serving, use external server like nginx etc.
return
}
// if file exists, just send it
file, err := os.Open(abs_path)
if err != nil{
fmt.Fprintf(con,"404 Not found (e)")
anlog.Error("Cannot open file `%s`. error: %s\n", abs_path,err.String())
return
}
defer file.Close()
bufsize := 1024*4
buff := make([]byte,bufsize+2)
sz, err := file.Read(buff)
if err != nil && err != os.EOF {
fmt.Fprintf(con,"404 Not found (e)")
anlog.Error("Cannot read %d bytes data in file `%s`. error: %s\n", sz, abs_path,err.String())
return
}
setHeaderCond(con, abs_path, buff)
// check for last-modified
//r.Header["If-Modified-Since"]
lm, _ := filemon.GetLastModif(file)
con.Header().Set("Last-Modified", lm)
if r.Header.Get("If-Modified-Since") == lm {
con.WriteHeader(http.StatusNotModified)
return
}
con.Write(buff[0:sz])
for {
sz, err := file.Read(buff)
if err != nil {
if err == os.EOF {
con.Write(buff[0:sz])
break
}
fmt.Fprintf(con,"404 Not found (e)")
anlog.Error("Cannot read %d bytes data in file `%s`. error: %s\n", sz, abs_path,err.String())
return
}
con.Write(buff[0:sz])
}
}
}
func ClearCacheHandler(c http.ResponseWriter, r *http.Request){
path_to_clear := r.FormValue("p")
if len(path_to_clear) == 0{
write(c,"Invalid parameter")
return
}
// prevent canonical path
if strings.HasPrefix(path_to_clear,"."){
write(c,"Bad path")
return
}
if path_to_clear[0] == '/'{
path_to_clear = path_to_clear[1:]
}
path_to_clear = "./data/" + path_to_clear
f, err := os.Open(path_to_clear)
if err != nil{
anlog.Error("File open error %s\n", err.String())
write(c,"Invalid request")
return
}
defer f.Close()
st, err := f.Stat()
if err != nil{
anlog.Error("Cannot stat file. error %s\n", err.String())
write(c,"Invalid request")
return
}
if !st.IsDirectory(){
write(c,"Invalid path")
return
}
err = os.RemoveAll(path_to_clear)
if err!=nil{
write(c,"Cannot clear path. e: %s", err.String())
return
}
store_dir := cfg.StoreDir
if path_to_clear == store_dir + "/"{
if err := os.Mkdir(store_dir,0775); err != nil{
anlog.Error("Cannot recreate base store_dir: `%s`\n", store_dir)
}
}
anlog.Info("Path cleared by request from `%s`: `%s`\n", r.Host, path_to_clear)
write(c,"Clear successfully")
}
func intro(){
fmt.Println("\n AnsCDN " + VERSION + " - a Simple CDN Server")
fmt.Println(" Copyright (C) 2010 Robin Syihab ([email protected])")
fmt.Println(" Under GPLv2 License\n")
}
func main() {
intro()
var cfg_file string
flag.StringVar(&cfg_file,"config","anscdn.cfg","Config file.")
flag.BoolVar(&quiet,"quiet",false,"Quiet.")
flag.Parse()
anlog.Quiet = quiet
var err os.Error
cfg, err = config.Parse(cfg_file)
cdnize.Cfg = cfg
if err != nil {
fmt.Println("Invalid configuration. e: ",err.String(),"\n")
os.Exit(1)
}
if len(cfg.BaseServer) == 0{
anlog.Error("No base server")
os.Exit(3)
}
if cfg.ServingPort == 0{
anlog.Error("No port")
os.Exit(4)
}
if len(cfg.StoreDir) == 0{
cfg.StoreDir = "./data"
}
if cfg.CacheExpires == 0{
cfg.CacheExpires = 1296000
}
fmt.Println("Configuration:")
fmt.Println("---------------------------------------")
fmt.Println("Base server: " + cfg.BaseServer)
if cfg.Strict == true {
fmt.Println("Strict mode ON")
}else{
fmt.Println("Strict mode OFF")
}
if cfg.CacheOnly == true {
fmt.Println("Cache only")
}
if cfg.IgnoreNoExt==true{fmt.Println("Ignore no extension files");}
if len(cfg.IgnoreExt)>0{fmt.Println("Ignore extension for", cfg.IgnoreExt);}
if len(cfg.ClearCachePath) > 0{
fmt.Println("Clear cache path: ", cfg.ClearCachePath)
}
fmt.Printf("Store cached data in `%s`\n", cfg.StoreDir)
if cfg.FileMon == true {
fmt.Println("File monitor enabled")
if err != nil{
anlog.Error("Invalid cache_expires value `%d`\n", cfg.CacheExpires)
os.Exit(5)
}
go filemon.StartFileMon(cfg.StoreDir, cfg.CacheExpires)
}
current_dir, _ := path.Split(os.Args[0])
os.Chdir(current_dir)
current_dir, err = os.Getwd()
if err != nil{
anlog.Error("Cannot get current_directory\n")
os.Exit(6)
}
anlog.Info("Current directory: %v\n", current_dir)
fi, err := os.Lstat(current_dir + cfg.StoreDir[1:])
if err != nil || fi.IsDirectory() == false{
err = os.Mkdir(current_dir + cfg.StoreDir[1:], 0755)
if err != nil{
anlog.Error("Cannot create dir `%s`. %s.\n", err)
os.Exit(8)
}
}
fmt.Println("---------------------------------------\n")
anlog.Info("Serving on 0.0.0.0:%d... ready.\n", cfg.ServingPort )
if len(cfg.ClearCachePath) > 0 {
if cfg.ClearCachePath[0] != '/'{
anlog.Error("Invalid ccp `%s`. missing `/`\n",cfg.ClearCachePath)
os.Exit(2)
}
http.Handle(cfg.ClearCachePath, http.HandlerFunc(ClearCacheHandler))
}
if cfg.ProvideApi == true {
http.Handle("/api/cdnize", http.HandlerFunc(cdnize.Handler))
fi, err := os.Lstat(current_dir + cfg.StoreDir[1:] + "/" + cfg.ApiStorePrefix)
if err != nil || fi.IsDirectory() == false{
err = os.Mkdir(current_dir + cfg.StoreDir[1:] + "/" + cfg.ApiStorePrefix, 0755)
if err != nil{
anlog.Error("Cannot create dir `%s`. %s.\n", err)
os.Exit(8)
}
}
http.Handle(fmt.Sprintf("/%s/", cfg.StoreDir[2:]), http.HandlerFunc(cdnize.StaticHandler))
}
http.Handle("/", http.HandlerFunc(MainHandler))
if err := http.ListenAndServe("0.0.0.0:" + strconv.Itoa(cfg.ServingPort), nil); err != nil {
anlog.Error("%s\n",err.String())
}
}