summary refs log tree commit diff stats
path: root/svc/handlers.go
blob: 02975ef6f7d72efb423d1d4307a37749692d4c11 (plain) (blame)
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
12
void parse_null_returns_null(void **state);
void parse_empty_returns_null(void **state);
void parse_space_returns_null(void **state);
void parse_cmd_no_args_returns_null(void **state);
void parse_cmd_with_space_returns_null(void **state);
void parse_cmd_with_too_few_returns_null(void **state);
void parse_cmd_with_too_many_returns_null(void **state);
void parse_cmd_one_arg(void **state);
void parse_cmd_two_args(void **state);
void parse_cmd_three_args(void **state);
void parse_cmd_three_args_with_spaces(void **state);
void parse_cmd_with_freetext(void **state);
void parse_cmd_one_arg_with_freetext(void **state);
void parse_cmd_two_args_with_freetext(void **state);
void parse_cmd_min_zero(void **state);
void parse_cmd_min_zero_with_freetext(void **state);
void parse_cmd_with_quoted(void **state);
void parse_cmd_with_quoted_and_space(void **state);
void parse_cmd_with_quoted_and_many_spaces(void **state);
void parse_cmd_with_many_quoted_and_many_spaces(void **state);
void parse_cmd_freetext_with_quoted(void **state);
void parse_cmd_freetext_with_quoted_and_space(void **state);
void parse_cmd_freetext_with_quoted_and_many_spaces(void **state);
void parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state);
void parse_cmd_with_quoted_freetext(void **state);
void parse_cmd_with_third_arg_quoted_0_min_3_max(void **state);
void parse_cmd_with_second_arg_quoted_0_min_3_max(void **state);
void parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state);
void count_one_token(void **state);
void count_one_token_quoted_no_whitespace(void **state);
void count_one_token_quoted_with_whitespace(void **state);
void count_two_tokens(void **state);
void count_two_tokens_first_quoted(void **state);
void count_two_tokens_second_quoted(void **state);
void count_two_tokens_both_quoted(void **state);
void get_first_of_one(void **state);
void get_first_of_two(void **state);
void get_first_two_of_three(void **state);
void get_first_two_of_three_first_quoted(void **state);
void get_first_two_of_three_second_quoted(void **state);
void get_first_two_of_three_first_and_second_quoted(void **state);
void parse_options_when_none_returns_empty_hasmap(void **state);
void parse_options_when_opt1_no_val_sets_error(void **state);
void parse_options_when_one_returns_map(void **state);
void parse_options_when_opt2_no_val_sets_error(void **state);
void parse_options_when_two_returns_map(void **state);
void parse_options_when_opt3_no_val_sets_error(void **state);
void parse_options_when_three_returns_map(void **state);
void parse_options_when_unknown_opt_sets_error(void **state);
void parse_options_with_duplicated_option_sets_error(void **state);
n>apiFormatHandler(w http.ResponseWriter, r *http.Request) { indexHandler(w, r) } func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryAllStatuses() if err != nil { log500(w, r, err) } data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // handles "/api/plain/(users|mentions|tweets)" func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { log500(w, r, err) return } if r.FormValue("q") != "" || r.FormValue("url") != "" { err := apiEndpointQuery(w, r) if err != nil { log500(w, r, err) return } log200(r) return } page := 1 pageVal := r.FormValue("page") if pageVal != "" { page, err = strconv.Atoi(pageVal) if err != nil || page == 0 { page = 1 } } // if there's no query, return everything in // registry for a given endpoint var out []string switch r.URL.Path { case "/api/plain/users": out, err = twtxtCache.QueryUser("") out = registry.ReduceToPage(page, out) case "/api/plain/mentions": out, err = twtxtCache.QueryInStatus("@<") out = registry.ReduceToPage(page, out) case "/api/plain/tweets": out, err = twtxtCache.QueryAllStatuses() out = registry.ReduceToPage(page, out) default: log404(w, r, fmt.Errorf("endpoint not found")) return } errLog("", err) data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // handles POST for "/api/plain/users" func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) { apiPostUser(w, r) } // handles "/api/plain/tags" func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryInStatus("#") if err != nil { log500(w, r, err) return } out = registry.ReduceToPage(1, out) data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // handles "/api/plain/tags/[a-zA-Z0-9]+" func apiTagsHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) tags := vars["tags"] out := compositeStatusQuery("#"+tags, r) out = registry.ReduceToPage(1, out) data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err := w.Write(data) if err != nil { log500(w, r, err) return } log200(r) }