summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
* commandlist: aliases are now references rather than copieshut2010-01-132-10/+35
* commandlist: no error when trying to alias to unexisting keyhut2010-01-131-5/+8
* commandlist: added alias(existing, *new)hut2010-01-132-0/+19
* Restructurization of commandshut2010-01-131-40/+39
* Added error handlers for failed commandshut2010-01-134-5/+20
* commandlist: added clear()hut2010-01-132-0/+14
* fsobject.directory: removed __getitem__ (too ambiguous)hut2010-01-133-9/+2
* browserview: hide certain bookmarks in the listhut2010-01-132-2/+4
* defaultui: clear statusbar message when opening consolehut2010-01-131-0/+1
* todo: added #37hut2010-01-131-0/+1
* commandlist: improvements and test casehut2010-01-132-6/+76
* updated pydoc documentationhut2010-01-1361-846/+795
* cleanups + style adjustmentshut2010-01-1313-56/+60
* added tentative tab completion for OpenConsolehut2010-01-131-2/+6
* console: added %s and %f metacharacters to OpenConsolehut2010-01-132-5/+51
* main: changed interrupt signal handlinghut2010-01-122-3/+52
* todo: added #36hut2010-01-121-0/+1
* keys: F1 displays helphut2010-01-121-1/+1
* ranger.py: more fixeshut2010-01-121-2/+2
* ranger.py: cleanup/fixhut2010-01-111-9/+8
* fixed #31, cd-after-exit works even after pressing ^Chut2010-01-112-2/+13
* main: change interrupt handler to curses.ungetch(CTRL_C)hut2010-01-113-3/+8
* keys: make ^C exit the programhut2010-01-112-13/+1
* commands: cleanup, extended docstringhut2010-01-111-0/+16
* added a way to create symlinkshut2010-01-113-2/+19
* actions: added an easier way to display errorshut2010-01-111-0/+3
* keys: changed paste key from 'p' to 'pp' (to avoid mistakes)hut2010-01-101-1/+2
* added chmod commandhut2010-01-102-1/+32
* help: added copyinghut2010-01-101-0/+18
* todo: added more info on bug #31hut2010-01-092-1/+6
* todo: added a few thingshut2010-01-091-0/+2
* actions: fixed deletion of symlinks to directorieshut2010-01-092-2/+2
* commands: fixed tabcompletionhut2010-01-091-2/+2
* todo: added #33hut2010-01-091-0/+1
* todo: added #32hut2010-01-081-0/+1
* added license informationhut2010-01-0863-3/+888
* directory: fixed non reloading symlinked directorieshut2010-01-072-3/+3
* applications: fixed bad redirection of stdouthut2010-01-071-1/+1
* random cleanups and fixeshut2010-01-076-13/+15
* directory: fixed wrong mtime comparisonhut2010-01-071-2/+5
* loader: fixed some issues with updating/redrawinghut2010-01-064-64/+82
* updated TODOhut2010-01-061-0/+7
* loader: don't pop items from queue which were added while loadinghut2010-01-061-1/+1
* actions: added reload_cwd + keybinding + helphut2010-01-063-0/+9
* curses_shortcuts: catch TypeError at addstrhut2010-01-061-2/+2
* help: extendedhut2010-01-062-6/+28
* help: improvedhut2010-01-062-5/+3
* color: added docstringhut2010-01-062-2/+17
* colorscheme: clean uphut2010-01-061-33/+56
* pager/help: added help markuphut2010-01-055-9/+69
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

















































































































































































































































































































































































































































































































                                                                                                                                 
02:29:20 -0400

database refs and static assets concurrency-safe' href='/gbmor/getwtxt/commit/svc/handlers.go?h=v0.5.0&id=e9d4a6b0e8624c6425d467f0efe14be1bc683bd9'>e9d4a6b ^
1168570 ^




3d2994b ^
1168570 ^




3d2994b ^
1168570 ^

90638ac ^


824556a ^
90638ac ^


e79fa24 ^
979e1f9 ^
90638ac ^

90638ac ^

824556a ^
90638ac ^

90638ac ^


1168570 ^

824556a ^
1168570 ^



824556a ^
04ad0e7 ^

1168570 ^
1168570 ^


824556a ^
90638ac ^

824556a ^
04ad0e7 ^
90638ac ^
824556a ^
90638ac ^



505c5d8 ^


37be25f ^

505c5d8 ^
90638ac ^
37be25f ^

4f0847b ^
90638ac ^
37be25f ^
a9d7d11 ^
505c5d8 ^
90638ac ^
a9d7d11 ^
910e69a ^
e79fa24 ^
910e69a ^




04ad0e7 ^

910e69a ^
910e69a ^

a9d7d11 ^
824556a ^
a9d7d11 ^
505c5d8 ^
78f4d8a ^
505c5d8 ^

e79fa24 ^
979e1f9 ^
a6f47c0 ^
dec003b ^
1168570 ^
505c5d8 ^
f4eb1c7 ^
824556a ^


1168570 ^
1168570 ^



df1d1ef ^
1168570 ^



dec003b ^

824556a ^
dec003b ^

1168570 ^
90638ac ^
dec003b ^
e79fa24 ^
979e1f9 ^
a6f47c0 ^
dec003b ^
1168570 ^
dec003b ^
f4eb1c7 ^
824556a ^
1168570 ^

1168570 ^




1168570 ^


eca8525 ^
90638ac ^
1168570 ^
e79fa24 ^
979e1f9 ^
a6f47c0 ^
1168570 ^
a6f47c0 ^
eca8525 ^
1168570 ^
824556a ^
1168570 ^

1168570 ^

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


















                                                                    
                                                      

        
             
                  
                  
                 
                 
              
 
                                     


                                
                                                
                                              
                       

                                            


                                  

                                            

 
                                                          
                    
                              

                                      



                              
                  






                                                            
         
 
                                              

                                                   
                  
 
 

                                                            
                                                    
                                                    

                                                                  
         




                                                             
                           




                                                               
                           

 


                                                                  
                                                                  


                                  
                             
 

                                               

                              
                                                                  

                      


                 

                                                                 
                                                                  



                                                               
                                                                          

                                 
                 


                      
                     

                                      
 
                          
                                                 
                                           



                                


                                                    

                                
                                                   
                                                      

                                   
                                                         
                                                      
 
                                 
                                                        
                                                      
 
                                  
                                             




                                                                               

                                 
                 

                      
                
                                                                                    
                      
         
                       

                                  
                             
 
                                    
                                               
 
                              
                       


                                                                  
         



                                                                     
                         



                                                                 

                                                 
                                                                  

                      
 
                                           
                                  
                             
 
                                    
                                               
 
                              
                       
                                                                  

                      




                                                             


                            
                                                
                                           
                                  
                             
 
                                    
                                               
 
                               
                       
                                                                  

                      

                 
/*
Copyright (c) 2019 Ben Morrison (gbmor)

This file is part of Getwtxt.

Getwtxt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Getwtxt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Getwtxt.  If not, see <https://www.gnu.org/licenses/>.
*/

package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"fmt"
	"hash/fnv"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/getwtxt/registry"
	"github.com/gorilla/mux"
)

func getEtagFromTime(modtime time.Time) string {
	shabytes, err := modtime.MarshalText()
	errLog("", err)
	bytes := fnv.New32().Sum(shabytes)
	return fmt.Sprintf("%x", bytes[:16])
}

func getEtag(data []byte) string {
	bytes := fnv.New32().Sum(data)
	return fmt.Sprintf("%x", bytes[:16])
}

func servStatic(w http.ResponseWriter, isCSS bool) error {
	pingAssets()
	staticCache.mu.RLock()
	defer staticCache.mu.RUnlock()

	var etag string
	var body []byte
	var contentType string

	if isCSS {
		etag = getEtagFromTime(staticCache.cssMod)
		contentType = cssutf8
		body = staticCache.css
	} else {
		etag = getEtagFromTime(staticCache.indexMod)
		contentType = htmlutf8
		body = staticCache.index
	}

	w.Header().Set("ETag", "\""+etag+"\"")
	w.Header().Set("Content-Type", contentType)
	_, err := w.Write(body)
	return err
}

// handles "/" and "/css"
func staticHandler(w http.ResponseWriter, r *http.Request) {
	isCSS := strings.Contains(r.URL.Path, "css")
	if err := servStatic(w, isCSS); err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
		return
	}
	log200(r)
}

// handles "/api"
func apiBaseHandler(w http.ResponseWriter, r *http.Request) {
	staticHandler(w, r)
}

// handles "/api/plain"
// maybe add json/xml support later
func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
	staticHandler(w, r)
}

func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
	out, err := twtxtCache.QueryAllStatuses()
	if err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
	}

	data := parseQueryOut(out)
	etag := getEtag(data)

	w.Header().Set("ETag", etag)
	w.Header().Set("Content-Type", txtutf8)
	_, err = w.Write(data)
	if err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
		return
	}
	log200(r)
}

// handles "/api/plain/(users|mentions|tweets)"
func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
	errLog("Error when parsing query values: ", r.ParseForm())

	if r.FormValue("q") != "" || r.FormValue("url") != "" {
		err := apiEndpointQuery(w, r)
		if err != nil {
			errHTTP(w, r, err, http.StatusInternalServerError)
		} else {
			log200(r)
		}
		return
	}

	var err error
	page := 1
	pageVal := r.FormValue("page")

	if pageVal != "" {
		page, err = strconv.Atoi(pageVal)
		if err != nil || page < 1 {
			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)

	case "/api/plain/version":
		etag := getEtag([]byte(Vers))
		w.Header().Set("ETag", "\""+etag+"\"")
		w.Header().Set("Content-Type", txtutf8)
		_, err := w.Write([]byte(strings.TrimSpace("getwtxt " + Vers)))
		if err != nil {
			errHTTP(w, r, err, http.StatusInternalServerError)
		} else {
			log200(r)
		}
		return

	default:
		errHTTP(w, r, fmt.Errorf("endpoint not found"), http.StatusNotFound)
		return
	}
	errLog("", err)

	data := parseQueryOut(out)
	etag := getEtag(data)

	w.Header().Set("ETag", etag)
	w.Header().Set("Content-Type", txtutf8)

	_, err = w.Write(data)
	if err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
	} else {
		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 {
		errHTTP(w, r, err, http.StatusInternalServerError)
		return
	}

	out = registry.ReduceToPage(1, out)
	data := parseQueryOut(out)
	etag := getEtag(data)

	w.Header().Set("ETag", etag)
	w.Header().Set("Content-Type", txtutf8)

	_, err = w.Write(data)
	if err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
		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 := getEtag(data)

	w.Header().Set("ETag", etag)
	w.Header().Set("Content-Type", txtutf8)

	_, err := w.Write(data)
	if err != nil {
		errHTTP(w, r, err, http.StatusInternalServerError)
		return
	}
	log200(r)
}