summaryrefslogtreecommitdiffstats
path: root/kbiff/kbiffurl.cpp
blob: e3b3f341973148f8b4ad56b9b85a65c621f2bd10 (plain)
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
/*
 * kbiffurl.cpp
 * Copyright (C) 1999-2002 Kurt Granroth <granroth@kde.org>
 * Copyright (C) 1999 Bjorn Hansson <Bjorn.Hansson@signal.uu.se>
 *
 * This file contains the implementation of KBiffURL and
 *
 * $Id$
 */
#include "kbiffurl.h"

KBiffURL::KBiffURL()
	: KURL()
{
}

KBiffURL::KBiffURL(const TQString& _url)
  : KURL(_url)
{
  /**
   * There exists no search part in the nntp spec; let's go around that
   */
  if (protocol() == "nntp")
  {
    TQString urlStr(_url);
    urlStr.replace(0,4, "imap4");
    *this = KBiffURL(urlStr);
    setProtocol("nntp");
  }
}

/**
 * BNF description of the syntax (see RFC1738, HTTP syntax)
 * ; HTTP
 *
 * httpurl        = "http://" hostport [ "/" hpath [ "?" search ]]
 * hpath          = hsegment *[ "/" hsegment ]
 * hsegment       = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
 * search         = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
 */
TQString KBiffURL::searchPar( const TQString & _parName ) const
{
  const char *_searchPart, *_searchPos, *_parVal;
  int n;

  // Set some initial values
  n = 0;
  _parVal = NULL;

  // Get the search part of the URL, i.e. the stuff after "?"
  TQString query_str(query());
  if (!query_str.isNull())
    query_str = query_str.remove(0,1);
  _searchPart = query_str.ascii();

  // Try to find the given parameter in the search part string
  if ((n = findPos( _searchPart, _parName )) < 0)
    return TQString::null;

  /*
   * Now, we know that the parameter name is found
   */

  // If no value is assigned, an empty string is returned
  if (_searchPart[n] != '=')
    return TQString::null;

  // Set the beginning of the parameter value string
  _parVal = &(_searchPart[n+1]);

  // Find the end of the assigned parameter value
  _searchPos = strpbrk( _parVal, ";:@&=" );

  // Return the assigned parameter value
  if (_searchPos == NULL)
    return TQString( _parVal );
  else
    return TQString( _parVal ).left(_searchPos - _parVal);
}

void KBiffURL::setSearchPar( const TQString & _parName, const TQString & _newParVal )
{
  TQString _newSearchPart;
  const char *_searchPart, *_searchPos, *_parVal;
  int index, len;

  // Get the search part of the URL, i.e. the stuff after "?"
  TQString query_str(query());

  if (!query_str.isNull())
    query_str = query_str.remove(0,1);
  _searchPart = query_str.ascii();

  // Try to find the given parameter in the search part string
  index = findPos( _searchPart, _parName );

  // If the parameter name is not found it is simply appended
  if (index < 0) 
  {
    if (query_str.length() > 0) 
    {
      _newSearchPart = query_str;
      _newSearchPart += "&";
    }
    _newSearchPart += _parName;
    _newSearchPart += "=";
    _newSearchPart += _newParVal;
  }
  else
  {
    _newSearchPart = _searchPart;

    // If no value is assigned, the new value is inserted
    if (_searchPart[index] != '=') 
    {
      _newSearchPart.insert( index, _newParVal );
      _newSearchPart.insert( index, '=' );
    }

    // Otherwise, the old value is replaced with the new one
    else
    {
      // Point to the first character of the assigned value
      index++;

      // Set the beginning of the parameter value string
      _parVal = &(_searchPart[index]);

      // Get the length of the old parameter value
      _searchPos = strpbrk( _parVal, ";:@&=" );

      if (_searchPos == NULL)
        len = strlen( _parVal );
      else
        len = _searchPos - _parVal;

      _newSearchPart.replace( index, len, _newParVal );
    }
  }

  setQuery( _newSearchPart );
}

int KBiffURL::findPos( const TQString & _searchPart, const TQString & _parName ) const
{
  const char *_searchPos;
  int n = -1;

  _searchPos = _searchPart.ascii();
  while (_searchPos != NULL)
  {
    _searchPos = strstr( _searchPos, _parName.ascii() );

    // If not found, a NULL string is returned
    if (_searchPos == NULL)
      return -1;

    // Find the index of the next character
    n = _searchPos - _searchPart.ascii() + strlen(_parName.ascii());

    // Stop searching if this is not a substring
    if ((_searchPos == _searchPart || _searchPos[-1] == '&') &&
        (_searchPart[n] == '0' || strchr(";:@&=", _searchPart[n]) != NULL))
      return n;

    _searchPos = &(_searchPart.ascii()[n+1]);
  }

  return -1;
}

TQString KBiffURL::pass() const
{
  return KURL::decode_string(KURL::pass());
}