OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "crypto/md5" | 5 "crypto/md5" |
6 "database/sql" | 6 "database/sql" |
7 "encoding/base64" | 7 "encoding/base64" |
8 "encoding/json" | 8 "encoding/json" |
9 "flag" | 9 "flag" |
10 "fmt" | 10 "fmt" |
(...skipping 16 matching lines...) Expand all Loading... |
27 RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR
_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -D
SK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_C
AN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_
SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I
../../tools/flags -I../../include/config -I../../include/core -I../../include/pa
thops -I../../include/pipe -I../../include/effects -I../../include/ports -I../..
/src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno
-exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unu
sed-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c
../../../cache/%s.cpp -o ../../../cache/%s.o` | 27 RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR
_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -D
SK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_C
AN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_
SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I
../../tools/flags -I../../include/config -I../../include/core -I../../include/pa
thops -I../../include/pipe -I../../include/effects -I../../include/ports -I../..
/src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno
-exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unu
sed-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c
../../../cache/%s.cpp -o ../../../cache/%s.o` |
28 LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start
-group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflag
s.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/
libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.
a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a li
bskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontcon
fig -ldl -lfreetype` | 28 LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start
-group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflag
s.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/
libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.
a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a li
bskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontcon
fig -ldl -lfreetype` |
29 DEFAULT_SAMPLE = `SkPaint p; | 29 DEFAULT_SAMPLE = `SkPaint p; |
30 p.setColor(SK_ColorRED); | 30 p.setColor(SK_ColorRED); |
31 p.setAntiAlias(true); | 31 p.setAntiAlias(true); |
32 p.setStyle(SkPaint::kStroke_Style); | 32 p.setStyle(SkPaint::kStroke_Style); |
33 p.setStrokeWidth(10); | 33 p.setStrokeWidth(10); |
34 | 34 |
35 canvas->drawLine(20, 20, 100, 100, p); | 35 canvas->drawLine(20, 20, 100, 100, p); |
36 ` | 36 ` |
| 37 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. |
| 38 MAX_TRY_SIZE = 64000 |
37 ) | 39 ) |
38 | 40 |
39 var ( | 41 var ( |
40 // codeTemplate is the cpp code template the user's code is copied into. | 42 // codeTemplate is the cpp code template the user's code is copied into. |
41 codeTemplate *template.Template = nil | 43 codeTemplate *template.Template = nil |
42 | 44 |
43 // indexTemplate is the main index.html page we serve. | 45 // indexTemplate is the main index.html page we serve. |
44 indexTemplate *htemplate.Template = nil | 46 indexTemplate *htemplate.Template = nil |
45 | 47 |
46 // recentTemplate is a list of recent images. | 48 // recentTemplate is a list of recent images. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 req.Header.Add("X-Google-Metadata-Request", "True") | 110 req.Header.Add("X-Google-Metadata-Request", "True") |
109 if resp, err := client.Do(req); err == nil { | 111 if resp, err := client.Do(req); err == nil { |
110 password, err := ioutil.ReadAll(resp.Body) | 112 password, err := ioutil.ReadAll(resp.Body) |
111 if err != nil { | 113 if err != nil { |
112 log.Printf("ERROR: Failed to read password from metadata
server: %q\n", err) | 114 log.Printf("ERROR: Failed to read password from metadata
server: %q\n", err) |
113 panic(err) | 115 panic(err) |
114 } | 116 } |
115 // The IP address of the database is found here: | 117 // The IP address of the database is found here: |
116 // https://console.developers.google.com/project/31977622648/
sql/instances/webtry/overview | 118 // https://console.developers.google.com/project/31977622648/
sql/instances/webtry/overview |
117 // And 3306 is the default port for MySQL. | 119 // And 3306 is the default port for MySQL. |
118 » » db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.8
3.52:3306)/webtry", password)) | 120 » » db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.8
3.52:3306)/webtry?parseTime=true", password)) |
119 if err != nil { | 121 if err != nil { |
120 log.Printf("ERROR: Failed to open connection to SQL serv
er: %q\n", err) | 122 log.Printf("ERROR: Failed to open connection to SQL serv
er: %q\n", err) |
121 panic(err) | 123 panic(err) |
122 } | 124 } |
123 } else { | 125 } else { |
124 // Fallback to sqlite for local use. | 126 // Fallback to sqlite for local use. |
125 db, err = sql.Open("sqlite3", "./webtry.db") | 127 db, err = sql.Open("sqlite3", "./webtry.db") |
126 if err != nil { | 128 if err != nil { |
127 log.Printf("ERROR: Failed to open: %q\n", err) | 129 log.Printf("ERROR: Failed to open: %q\n", err) |
128 panic(err) | 130 panic(err) |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 log.Printf("Error: failed to fetch from database: %q", e
rr) | 287 log.Printf("Error: failed to fetch from database: %q", e
rr) |
286 continue | 288 continue |
287 } | 289 } |
288 recent = append(recent, Try{Hash: hash, CreateTS: create_ts.Form
at("2006-02-01")}) | 290 recent = append(recent, Try{Hash: hash, CreateTS: create_ts.Form
at("2006-02-01")}) |
289 } | 291 } |
290 if err := recentTemplate.Execute(w, Recent{Tries: recent}); err != nil { | 292 if err := recentTemplate.Execute(w, Recent{Tries: recent}); err != nil { |
291 log.Printf("ERROR: Failed to expand template: %q\n", err) | 293 log.Printf("ERROR: Failed to expand template: %q\n", err) |
292 } | 294 } |
293 } | 295 } |
294 | 296 |
| 297 // hasPreProcessor returns true if any line in the code begins with a # char. |
| 298 func hasPreProcessor(code string) bool { |
| 299 lines := strings.Split(code, "\n") |
| 300 for _, s := range lines { |
| 301 if strings.HasPrefix(strings.TrimSpace(s), "#") { |
| 302 return true |
| 303 } |
| 304 } |
| 305 return false |
| 306 } |
| 307 |
295 // mainHandler handles the GET and POST of the main page. | 308 // mainHandler handles the GET and POST of the main page. |
296 func mainHandler(w http.ResponseWriter, r *http.Request) { | 309 func mainHandler(w http.ResponseWriter, r *http.Request) { |
297 log.Printf("Main Handler: %q\n", r.URL.Path) | 310 log.Printf("Main Handler: %q\n", r.URL.Path) |
298 if r.Method == "GET" { | 311 if r.Method == "GET" { |
299 code := DEFAULT_SAMPLE | 312 code := DEFAULT_SAMPLE |
300 match := directLink.FindStringSubmatch(r.URL.Path) | 313 match := directLink.FindStringSubmatch(r.URL.Path) |
301 if len(match) == 2 && r.URL.Path != "/" { | 314 if len(match) == 2 && r.URL.Path != "/" { |
302 hash := match[1] | 315 hash := match[1] |
303 if db == nil { | 316 if db == nil { |
304 http.NotFound(w, r) | 317 http.NotFound(w, r) |
305 return | 318 return |
306 } | 319 } |
307 // Update 'code' with the code found in the database. | 320 // Update 'code' with the code found in the database. |
308 if err := db.QueryRow("SELECT code FROM webtry WHERE has
h=?", hash).Scan(&code); err != nil { | 321 if err := db.QueryRow("SELECT code FROM webtry WHERE has
h=?", hash).Scan(&code); err != nil { |
309 http.NotFound(w, r) | 322 http.NotFound(w, r) |
310 return | 323 return |
311 } | 324 } |
312 } | 325 } |
313 // Expand the template. | 326 // Expand the template. |
314 if err := indexTemplate.Execute(w, userCode{UserCode: code}); er
r != nil { | 327 if err := indexTemplate.Execute(w, userCode{UserCode: code}); er
r != nil { |
315 log.Printf("ERROR: Failed to expand template: %q\n", err
) | 328 log.Printf("ERROR: Failed to expand template: %q\n", err
) |
316 } | 329 } |
317 } else if r.Method == "POST" { | 330 } else if r.Method == "POST" { |
318 w.Header().Set("Content-Type", "application/json") | 331 w.Header().Set("Content-Type", "application/json") |
319 » » b, err := ioutil.ReadAll(r.Body) | 332 » » buf := bytes.NewBuffer(make([]byte, 0, MAX_TRY_SIZE)) |
| 333 » » n, err := buf.ReadFrom(r.Body) |
320 if err != nil { | 334 if err != nil { |
321 reportError(w, r, err, "Failed to read a request body.") | 335 reportError(w, r, err, "Failed to read a request body.") |
322 return | 336 return |
323 } | 337 } |
324 » » code := string(b) | 338 » » if n == MAX_TRY_SIZE { |
| 339 » » » err := fmt.Errorf("Code length equal to, or exceeded, %d
", MAX_TRY_SIZE) |
| 340 » » » reportError(w, r, err, "Code too large.") |
| 341 » » » return |
| 342 » » } |
| 343 » » code := string(buf.Bytes()) |
| 344 » » if hasPreProcessor(code) { |
| 345 » » » err := fmt.Errorf("Found preprocessor macro in code.") |
| 346 » » » reportError(w, r, err, "Preprocessor macros aren't allow
ed.") |
| 347 » » » return |
| 348 » » } |
325 hash, err := expandCode(LineNumbers(code)) | 349 hash, err := expandCode(LineNumbers(code)) |
326 if err != nil { | 350 if err != nil { |
327 reportError(w, r, err, "Failed to write the code to comp
ile.") | 351 reportError(w, r, err, "Failed to write the code to comp
ile.") |
328 return | 352 return |
329 } | 353 } |
330 writeToDatabase(hash, code) | 354 writeToDatabase(hash, code) |
331 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t
rue) | 355 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t
rue) |
332 if err != nil { | 356 if err != nil { |
333 reportError(w, r, err, "Failed to compile the code:\n"+m
essage) | 357 reportError(w, r, err, "Failed to compile the code:\n"+m
essage) |
334 return | 358 return |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 401 } |
378 | 402 |
379 func main() { | 403 func main() { |
380 flag.Parse() | 404 flag.Parse() |
381 http.HandleFunc("/i/", imageHandler) | 405 http.HandleFunc("/i/", imageHandler) |
382 http.HandleFunc("/recent/", recentHandler) | 406 http.HandleFunc("/recent/", recentHandler) |
383 http.HandleFunc("/css/", cssHandler) | 407 http.HandleFunc("/css/", cssHandler) |
384 http.HandleFunc("/", mainHandler) | 408 http.HandleFunc("/", mainHandler) |
385 log.Fatal(http.ListenAndServe(*port, nil)) | 409 log.Fatal(http.ListenAndServe(*port, nil)) |
386 } | 410 } |
OLD | NEW |