aboutsummaryrefslogtreecommitdiffstats
path: root/src/_httpie
blob: dce6100aaa8f18ac00a9ffab8b77cd709aeaa771 (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
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
#compdef http
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for httpie 0.7.2  (http://httpie.org)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Akira Maeda <https://github.com/glidenote>
#  * Valodim <https://github.com/Valodim>
#
# ------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------

_httpie_params () {

    local ret=1 expl

    # or a url
    if (( CURRENT <= NORMARG+1 )) && [[ $words[NORMARG] != *:* ]] ; then
        _httpie_urls && ret=0

    # regular param, if we already have a url
    elif (( CURRENT > NORMARG )); then

        # if the suffix is precisely : this is shorthand for a header
        if [[ -prefix ':' ]]; then
            PREFIX=
            SUFFIX=:
        fi

        # if we are in front of a : (possibly due to the PREFIX move before)
        if [[ -suffix ':' ]]; then

            # this is rather buggy with normal tab behavior :\
            compstate[insert]=menu
            _wanted http_header expl 'HTTP Header' \
                compadd -s ':' -S '' -- Content-Type Cookie && return 0
        fi

        # ignore all prefix stuff
        compset -P '(#b)([^:@=]#)'
        local name=$match[1]

        if compset -P ':'; then
            _message "$name header content"
        elif compset -P '@'; then
            _files
        elif compset -P ':='; then
            _message "$name raw json data"
        elif compset -P '=='; then
            _message "$name url parameter value"
        elif compset -P '='; then
            _message "$name data field value"
        else
            typeset -a ops
            ops=(
                '=:data field'
                '\::header'
                '==:request parameter'
                '@:data file field'
                '\:=:raw json field'
            )
            _describe -t httpparams "parameter types" ops -Q -S ''
        fi

        ret=0

    fi

    # first arg may be a request method
    (( CURRENT == NORMARG )) &&
        _wanted http_method expl 'Request Method' \
            compadd GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK && ret=0

    return $ret

}

_httpie_urls() {

  local ret=1

  if ! [[ -prefix [-+.a-z0-9]#:// ]]; then
    local expl
    compset -S '[^:/]*' && compstate[to_end]=''
    _wanted url-schemas expl 'URL schema' compadd -S '' http:// https:// && ret=0
  else
    _urls && ret=0
  fi

  return $ret

}

_httpie_printflags () {

    local ret=1

    # not sure why this is necessary, but it will complete "-pH" style without it
    [[ $IPREFIX == "-p" ]] && IPREFIX+=" "

    compset -P '(#b)([a-zA-Z]#)'

    local -a flags
    [[ $match[1] != *H* ]] && flags+=( "H:request headers" )
    [[ $match[1] != *B* ]] && flags+=( "B:request body" )
    [[ $match[1] != *h* ]] && flags+=( "h:response headers" )
    [[ $match[1] != *b* ]] && flags+=( "b:response body" )

    _describe -t printflags "print flags" flags -S '' && ret=0

    return $ret

}

integer NORMARG

_arguments -n -C -s \
  '(-f)'{-j,--json}'[Data items from the command line are serialized as a JSON object.]' \
  '(-j)'{-f,--form}'[Data items from the command line are serialized as form fields.]' \
  '--pretty[Controls output processing.]:PRETTY:(all colors format none)' \
  '(-s --style)'{-s,--style}'[Output coloring style]:STYLE:(autumn borland bw colorful default emacs friendly fruity manni monokai murphy native pastie perldoc ttr solarized tango trac vim vs)' \
  '(-p --print)'{-p,--print}'=[String specifying what the output should contain]:print flags:_httpie_printflags' \
  '(-v --verbose)'{-v,--verbose}'[Print the whole request as well as the response.]' \
  '(-p)'{-h,--headers}'[Print only the response headers.]' \
  '(-p)'{-b,--body}'[Print only the response body.]' \
  '(-S --stream)'{-S,--stream}'[Always stream the output by line, i.e., behave like `tail -f`.]' \
  '(-o --output)'{-o,--output}'[Save output to FILE.]:FILE:_files' \
  '(-d --download)'{-d,--download}'[Do not print the response body to stdout.]' \
  '(-c --continue)'{-c,--continue}'[Resume an interrupted download.]' \
  '--session[Create, or reuse and update a session.]:SESSION_NAME_OR_PATH' \
  '--session-read-only[Create or read a session without updating it form the request/response exchange.]:SESSION_NAME_OR_PATH' \
  '(-a --auth)'{-a,--auth}'[If only the username is provided (-a username)]:USER\:PASS' \
  '--auth-type[The authentication mechanism to be used. Defaults to "basic".]:AUTH-TYPE:(basic digest)' \
  '--proxy[String mapping protocol to the URL of the proxy (e.g.http:foo.bar:3128).]:PROXY' \
  '--follow[Set this flag if full redirects are allowed]' \
  "--verify[Set to \"no\" to skip checking the host's SSL certificate.]:verify certificate:(yes no)" \
  '--allow-redirects[Set this flag if full redirects are allowed (e.g. re-POST-ing of data at new ``Location``)]' \
  '--timeout[Float describes the timeout of the request (Use socket.setdefaulttimeout() as fallback).]:timeout (seconds)' \
  '--check-status[This flag instructs HTTPie to also check the HTTP status code and exit with an error if the status indicates one.]' \
  '--ignore-stdin[Do not attempt to read stdin.]' \
  '--help[show help message.]' \
  "--version[show program's version number and exit.]" \
  '--traceback[Prints exception traceback should one occur.]' \
  '--debug[Prints exception traceback should one occur and other information useful for debugging HTTPie itself.]' \
  '*:args:_httpie_params' && return 0