URLveneer 13.7 KB
Newer Older
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
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : URLveneer.c                            */
/*                                                 */
/* Purpose: Veneer to the URL_Fetcher module SWIs. */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 17-Aug-97: Created from Fetch.c.       */
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"

#include "wimp.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

#include "URLutils.h"

#include "URLveneer.h"

/*************************************************/
/* url_register()                                */
/*                                               */
/* Registers a requirement to fetch a URL with   */
/* the URL module.                               */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             Pointer to int into which the     */
/*             session handle is placed. May be  */
/*             NULL.                             */
/*************************************************/

_kernel_oserror * url_register(unsigned int flags, unsigned int * handle)
{
  _kernel_oserror * e;
  unsigned int      h;

  /* If there's a pointer to put the handle into, set it to zero initially */

  if (handle) *handle = 0;

  e = _swix(URL_Register,
            _IN(0) | _OUT(1),

            flags,

            &h);

  /* If the call didn't return an error, store the session handle */

  if ((!e) && (handle)) *handle = h;

  #ifdef TRACE
    if (tl & (1u<<6))
    {
      if (!e) Printf("url_register: Registered ID %d\n",*handle);
      else Printf("url_register: Exitting with error\n");
    }
  #endif

  return e;
}

/*************************************************/
/* url_deregister()                              */
/*                                               */
/* Deregisters a requirement to fetch a URL with */
/* the URL module.                               */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             The session handle.               */
/*************************************************/

_kernel_oserror * url_deregister(unsigned int flags, unsigned int handle)
{
  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_deregister: Deregistering ID %d\n",handle);
  #endif

  /* Abort any current action */

  url_stop(flags,handle);

  /* Deregister the session */

  return _swix(URL_Deregister,
               _INR(0,1),

               flags,
               handle);
}

/*************************************************/
/* url_stop()                                    */
/*                                               */
/* Interrupts a fetch if one is going on.        */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             The session handle.               */
/*************************************************/

_kernel_oserror * url_stop(unsigned int flags, unsigned int handle)
{
  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_stop: Stop with ID %d\n",handle);
  #endif

  return _swix(URL_Stop,
               _INR(0,1),

               flags,
               handle);
}

/*************************************************/
/* url_get_url()                                 */
/*                                               */
/* Starts fetching data from a URL.              */
/*                                               */
144 145 146 147 148
/* Parameters: Flags - if URL_GetURL_AgentGiven  */
/*             (see URLveneer.h) is set, this    */
/*             function works out the agent      */
/*             string so you don't need to pass  */
/*             it in externally;                 */
149 150 151
/*                                               */
/*             The session handle;               */
/*                                               */
152 153
/*             The fetch method, as in html_get  */
/*             in FetchHTML.c;                   */
154 155 156
/*                                               */
/*             Pointer to URL to fetch;          */
/*                                               */
157 158 159 160 161
/*             Pointer to a pointer to any extra */
/*             data to send for POST etc. (this  */
/*             allows such data to be stored in  */
/*             a flex block - you'd pass the     */
/*             pointer to the flex anchor);      */
162 163 164 165 166 167 168 169 170 171 172 173 174
/*                                               */
/*             Pointer to an int into which a    */
/*             status flag is placed (this may   */
/*             be NULL);                         */
/*                                               */
/*             The fetch mode:                   */
/*                                               */
/*             0: Get data only,                 */
/*             1: Get header only,               */
/*             2: Get both.                      */
/*************************************************/

_kernel_oserror * url_get_url(unsigned int flags, unsigned int handle, int method, char * url,
175
                              char ** extradata, unsigned int * status, int mode)
176
{
177
  char              agent[Limits_UserAgent];
178 179 180
  _kernel_oserror * e = NULL;
  int               s;

181 182
  agent[0] = '\0';

183 184 185 186 187 188 189 190 191
  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_get_url: Called with ID %d\n",handle);
  #endif

  /* If a pointer to the int in which status information can be written */
  /* is not NULL, set the current status to 0                           */

  if (status) *status = 0;

192
  /* Only proceed if we have a URL, and it's not an internal one */
193

194
  if (url && strncmp(url, Internal_URL, strlen(Internal_URL)))
195 196 197 198 199 200 201
  {
    #ifdef DUMP_HEADERS
      {
        FILE * file;

        file = fopen("<Wimp$ScrapDir>.Headers", "ab");

202
        if (!extradata || !*extradata || !**extradata) fprintf(file, "Fetch URL '%s'; sending standard header\r\n\r\n", url);
203 204
        else
        {
205 206
          fprintf(file, "Fetch URL '%s'; sending standard header plus:\r\n\r\n%s\r\n", url, *extradata);
          if ((*extradata)[strlen(*extradata) - 1] != '\n') fprintf(file, "\r\n");
207 208 209 210 211 212
        }

        fclose(file);
      }
    #endif

213 214
    /* We may need to work out the user agent string */

215
    if (flags & URL_GetURL_AgentGiven)
216
    {
217
      utils_build_user_agent_string(choices.clone, agent, sizeof(agent));
218 219 220 221 222
    }

    /* Now call the SWI - it can take a while to complete, */
    /* so turn the hourglass on.                           */

223 224 225
    _swix(Hourglass_Start, _IN(0), 50);

    e = _swix(URL_GetURL,
226
              _INR(0,6) | _OUT(0),
227 228 229 230 231

              flags,
              handle,
              method,
              url,
232
              *extradata,
233
              mode,
234
              agent,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

              &s);

    _swix(Hourglass_Off, 0);

    /* If the GetURL call didn't return an error, write the new status */

    if (status && !e) *status = s;
  }

  #ifdef TRACE
    if (tl & (1u<<6))
    {
      if (!e) Printf("url_get_url: Successful\n");
      else Printf("url_get_url: Exitting with error\n");
    }
  #endif

  return e;
}

/*************************************************/
/* url_read_data()                               */
/*                                               */
/* Asks the URL module to copy some of the data  */
/* it has fetched over into a buffer.            */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             The session handle;               */
/*                                               */
/*             Pointer to buffer into which the  */
/*             data is transferred, or NULL to   */
/*             just get a Pending state;         */
/*                                               */
/*             The size of the buffer;           */
/*                                               */
/*             Pointer to an int, into which a   */
/*             status word is placed;            */
/*                                               */
/*             Pointer to int, into which the    */
/*             number of bytes read (and put in  */
/*             the bufrer) is placed;            */
/*                                               */
/*             Pointer to int, into which the    */
/*             number of bytes that are still to */
/*             be fetched s placed.              */
/*************************************************/

_kernel_oserror * url_read_data(unsigned int flags, unsigned int handle, void * buffer,
                                int size, int * status, int * read, int * pending)
{

  _kernel_oserror * e;
  int               s, r, p = 0;

  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_read_data: Called with ID %d\n",handle);
  #endif

  /* Ensure all returned data is set to a sensible default to start with */

  if (status)  * status  = 0;
  if (read)    * read    = 0;
  if (pending) * pending = 0;

  /* Call the URL module */

  e = _swix(URL_ReadData,
            _INR(0,3) | _OUT(0) | _OUTR(4,5),

            flags,
            handle,
            buffer,
            size,

            &s,
            &r,
            &p);

  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_read_data: Status %p, error %p returned\n",s,e);
  #endif

  /* In the absence of any errors, fill in the relevant returned data */

  if (!e)
  {
    if (status)  *status  = s;
    if (read)    *read    = r;
    if (pending) *pending = p;
  }

  #ifdef TRACE
    if (tl & (1u<<6))
    {
      if (!e) Printf("url_read_data: Successful\n");
      else Printf("url_read_data: Exitting with error\n");
    }
  #endif

  return e;
}

/*************************************************/
/* url_status()                                  */
/*                                               */
/* Returns the status of a fetch.                */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             The session handle;               */
/*                                               */
/*             Pointer to an int, into which a   */
/*             status word is placed;            */
/*                                               */
/*             Pointer to an int, into which the */
/*             server's response is placed;      */
/*                                               */
/*             Pointer to an int, into which the */
/*             number of bytes transferred so    */
/*             far is placed.                    */
/*************************************************/

_kernel_oserror * url_status(unsigned int flags, unsigned int handle,
                             int * status, int * response, int * bytes)
{
  _kernel_oserror * e;
  int               s, r, b;

  #ifdef TRACE
    if (tl & (1u<<6)) Printf("url_status: Called with ID %d\n",handle);
  #endif

  /* Set returned data to zero to begin with */

  if (status)   *status   = 0;
  if (response) *response = 0;
  if (bytes)    *bytes    = 0;

  e = _swix(URL_Status,
            _INR(0,1) | _OUT(0) | _OUTR(2,3),

            flags,
            handle,

            &s,
            &r,
            &b);

  /* In the absence of an error, fill in the returned data */

  if (!e)
  {
    if (status)   *status   = s;
    if (response) *response = r;
    if (bytes)    *bytes    = b;
  }

  #ifdef TRACE
    if (tl & (1u<<6))
    {
      if (!e) Printf("url_status: Successful\n");
      else Printf("url_status: Exitting with error\n");
    }
  #endif

  return e;
}

/*************************************************/
/* url_set_proxy()                               */
/*                                               */
/* Instructs the URL module to fetch through a   */
/* proxy (or not).                               */
/*                                               */
/* Parameters: Flags (must be 0 at present);     */
/*                                               */
/*             The session handle;               */
/*                                               */
/*             Pointer to the base URL of the    */
/*             proxy server, e.g. for a local    */
/*             proxy, "http://127.0.0.1/";       */
/*                                               */
/*             Pointer to a string holding the   */
/*             protocol to use, e.g. "http:" or  */
/*             "ftp:";                           */
/*                                               */
/*             1 to disable proxying, 0 to       */
/*             enable it with the above data.    */
/*************************************************/

_kernel_oserror * url_set_proxy(int flags, unsigned int session, char * baseurl,
                                char * protocol, int noproxy)
{
  return _swix(URL_SetProxy,
               _INR(0,4),

               flags,
               session,
               baseurl,
               protocol,
               noproxy);
}