Skip to content

Commit bc54347

Browse files
committed
Add SSL check to rails#get_binding_for
* Attempted to get win32 working using curl since I could not figure out a better way to determine this in windows, and I am not familar with the common ways windows users install/use ruby on Windows other than through WSL2. I used railsinstaller to accomplish this, which comes with curl. If curl does not exist, it reverts to http as the default. * Used @tpope's recommendation of using the pid to retrieve the process for non-win32 systems. * Prepended the uri_scheme to the return values of get_binding_for(pid)
1 parent e49f4ed commit bc54347

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

autoload/rails.vim

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,17 +1742,16 @@ function! s:Preview(bang, lnum, uri) abort
17421742
endif
17431743
let binding = s:sub(binding, '^0\.0\.0\.0>|^127\.0\.0\.1>', 'localhost')
17441744
let binding = s:sub(binding, '^\[::\]', '[::1]')
1745-
let uri_scheme = rails#app().uri_scheme()
17461745
let uri = empty(a:uri) ? get(rails#buffer().preview_urls(a:lnum),0,'') : a:uri
17471746
if uri =~ '://'
17481747
"
17491748
elseif uri =~# '^[[:alnum:]-]\+\.'
1750-
let uri = uri_scheme.'://'.s:sub(uri, '^[^/]*\zs', matchstr(root, ':\d\+$'))
1749+
let uri = s:sub(uri, '^[^/]*\zs', matchstr(root, ':\d\+$'))
17511750
elseif uri =~# '^[[:alnum:]-]\+\%(/\|$\)'
17521751
let domain = s:sub(binding, '^localhost>', 'lvh.me')
1753-
let uri = uri_scheme.'://'.s:sub(uri, '^[^/]*\zs', '.'.domain)
1752+
let uri = s:sub(uri, '^[^/]*\zs', '.'.domain)
17541753
else
1755-
let uri = uri_scheme.'://'.binding.'/'.s:sub(uri,'^/','')
1754+
let uri = binding.'/'.s:sub(uri,'^/','')
17561755
endif
17571756
call s:initOpenURL()
17581757
if (exists(':OpenURL') == 2) && !a:bang
@@ -1962,10 +1961,21 @@ function! rails#get_binding_for(pid) abort
19621961
if empty(a:pid)
19631962
return ''
19641963
endif
1964+
let uri_scheme = 'http://'
19651965
if has('win32')
19661966
let output = system('netstat -anop tcp')
19671967
let binding = matchstr(output, '\n\s*TCP\s\+\zs\S\+\ze\s\+\S\+\s\+LISTENING\s\+'.a:pid.'\>')
1968-
return s:sub(binding, '^([^[]*:.*):', '[\1]:')
1968+
if executable('curl')
1969+
let curl_result = system('curl --silent --head --fail https://'.binding)
1970+
if len(matchstr(curl_result, '200 OK')) > 0
1971+
let uri_scheme = 'https://'
1972+
endif
1973+
endif
1974+
return uri_scheme.s:sub(binding, '^([^[]*:.*):', '[\1]:')
1975+
endif
1976+
let has_ssl = system('ps -p '.a:pid.' | grep "ssl:"')
1977+
if len(has_ssl) > 0
1978+
let uri_scheme = 'https://'
19691979
endif
19701980
if executable('lsof')
19711981
let lsof = 'lsof'
@@ -1981,12 +1991,12 @@ function! rails#get_binding_for(pid) abort
19811991
let binding = matchstr(output, '\S\+:\d\+\ze\s\+(LISTEN)\n')
19821992
let binding = s:sub(binding, '^\*', '[::]')
19831993
endif
1984-
return binding
1994+
return uri_scheme.binding
19851995
endif
19861996
if executable('netstat')
19871997
let output = system('netstat -antp')
19881998
let binding = matchstr(output, '\S\+:\d\+\ze\s\+\S\+\s\+LISTEN\s\+'.a:pid.'/')
1989-
return s:sub(binding, '^([^[]*:.*):', '[\1]:')
1999+
return uri_scheme.s:sub(binding, '^([^[]*:.*):', '[\1]:')
19902000
endif
19912001
return ''
19922002
endfunction
@@ -2681,29 +2691,15 @@ function! s:app_named_route_file(route_name) dict abort
26812691
return ""
26822692
endfunction
26832693

2684-
function! s:app_uri_scheme() dict abort
2685-
if self.cache.needs('uri_scheme')
2686-
let binding = self.server_binding()
2687-
let https = system('ps | grep -v "grep" | grep ' . shellescape('ssl://' . binding))
2688-
if len(https) > 0
2689-
call self.cache.set('uri_scheme', 'https')
2690-
else
2691-
call self.cache.set('uri_scheme', 'http')
2692-
endif
2693-
endif
2694-
return self.cache.get('uri_scheme')
2695-
endfunction
2696-
26972694
function! s:app_routes() dict abort
26982695
if self.cache.needs('routes')
26992696
let cd = haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'
27002697
let cwd = getcwd()
27012698
let routes = []
27022699
let paths = {}
27032700
let binding = self.server_binding()
2704-
let uri_scheme = self.uri_scheme()
27052701
if len(binding) && len(s:webcat())
2706-
let html = system(s:webcat() . ' ' . shellescape(uri_scheme . '://' . binding . '/rails/info/routes'))
2702+
let html = system(s:webcat() . ' ' . shellescape(binding . '/rails/info/routes'))
27072703
for line in split(matchstr(html, '.*<tbody>\zs.*\ze</tbody>'), "\n")
27082704
let val = matchstr(line, '\C<td data-route-name=''\zs[^'']*''\ze>')
27092705
if len(val)
@@ -2760,7 +2756,7 @@ function! s:app_routes() dict abort
27602756
return self.cache.get('routes')
27612757
endfunction
27622758

2763-
call s:add_methods('app', ['routes', 'named_route_file', 'uri_scheme'])
2759+
call s:add_methods('app', ['routes', 'named_route_file'])
27642760

27652761
" }}}1
27662762
" Projection Commands {{{1

0 commit comments

Comments
 (0)