~ubuntu-branches/ubuntu/edgy/ncbi-tools6/edgy

« back to all changes in this revision

Viewing changes to connect/urlquery.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-19 23:28:07 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060719232807-et3cdmcjgmnyleyx
Tags: 6.1.20060507-3ubuntu1
Re-merge with Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*   urlquery.c
2
 
* ===========================================================================
3
 
*
4
 
*                            PUBLIC DOMAIN NOTICE
5
 
*            National Center for Biotechnology Information (NCBI)
6
 
*
7
 
*  This software/database is a "United States Government Work" under the
8
 
*  terms of the United States Copyright Act.  It was written as part of
9
 
*  the author's official duties as a United States Government employee and
10
 
*  thus cannot be copyrighted.  This software/database is freely available
11
 
*  to the public for use. The National Library of Medicine and the U.S.
12
 
*  Government do not place any restriction on its use or reproduction.
13
 
*  We would, however, appreciate having the NCBI and the author cited in
14
 
*  any work or product based on this material
15
 
*
16
 
*  Although all reasonable efforts have been taken to ensure the accuracy
17
 
*  and reliability of the software and data, the NLM and the U.S.
18
 
*  Government do not and cannot warrant the performance or results that
19
 
*  may be obtained by using this software or data. The NLM and the U.S.
20
 
*  Government disclaim all warranties, express or implied, including
21
 
*  warranties of performance, merchantability or fitness for any particular
22
 
*  purpose.
23
 
*
24
 
* ===========================================================================
25
 
*
26
 
* File Name:  urlquery.c
27
 
*
28
 
* Author:  Jonathan Kans
29
 
*
30
 
* Version Creation Date:   4/16/98
31
 
*
32
 
* $Revision: 6.33 $
33
 
*
34
 
* File Description: 
35
 
*
36
 
* Modifications:  
37
 
* --------------------------------------------------------------------------
38
 
* $Log: urlquery.c,v $
39
 
* Revision 6.33  2005/12/01 18:47:49  lavr
40
 
* Code formatting
41
 
*
42
 
* Revision 6.32  2004/09/16 19:16:45  lavr
43
 
* QUERY_OpenUrlQuery() to set port only if provided as non-zero
44
 
*
45
 
* Revision 6.31  2004/09/14 19:09:41  lavr
46
 
* Correct last change log entry
47
 
*
48
 
* Revision 6.30  2004/09/14 18:52:12  kans
49
 
* QUERY_OpenUrlQuery makes content type only if type < eMIME_T_Unknown
50
 
*
51
 
* Revision 6.29  2004/09/14 18:47:51  kans
52
 
* QUERY_OpenUrlQuery looks for eMIME_T_Unknown, does not compost content type
53
 
*
54
 
* Revision 6.28  2004/02/23 15:30:02  lavr
55
 
* New (last) parameter "how" added in CONN_Write() API call
56
 
*
57
 
* Revision 6.27  2003/10/21 18:27:43  lavr
58
 
* QUERY_OpenServiceQuery(): Timeout override changed
59
 
*
60
 
* Revision 6.26  2003/09/03 21:15:29  lavr
61
 
* Reuse "arguments" in QUERY_OpenServiceQuery() to be real service argument
62
 
* (formely it was to modify the dispatcher and was not really used anywhere)
63
 
*
64
 
* Revision 6.25  2003/05/29 21:54:20  kans
65
 
* QUERY_CheckQueue calls callback and dequeues connection if status is eIO_Closed as well as eIO_Success  - callbacks already check status before trying to read
66
 
*
67
 
* Revision 6.24  2003/05/29 19:02:56  kans
68
 
* badstatus only used for future debugging purposes, does not block further checks, also increments count of remaining queued connections for return value
69
 
*
70
 
* Revision 6.23  2003/05/29 18:39:54  kans
71
 
* QUERY_CheckQueue sets new badstatus field, protects against further checks, if eIO_Timeout or eIO_Closed
72
 
*
73
 
* Revision 6.22  2002/11/21 20:35:41  kans
74
 
* forgot to call ConnNetInfo_Destroy if bailing due to NULL connector
75
 
*
76
 
* Revision 6.21  2002/11/21 19:46:32  kans
77
 
* if connector is NULL, do not call CONN_Create
78
 
*
79
 
* Revision 6.20  2002/08/07 18:45:17  lavr
80
 
* Change from deprecated to current EIO_ReadMethod enums
81
 
*
82
 
* Revision 6.19  2002/07/02 17:17:58  kans
83
 
* fixed error message
84
 
*
85
 
* Revision 6.18  2001/09/25 13:20:56  lavr
86
 
* SERVICE_CreateConnectorEx() - number of args adjusted
87
 
*
88
 
* Revision 6.17  2001/08/16 18:07:04  kans
89
 
* protect against calling CONN_Read or CONN_Write with NULL conn parameter
90
 
*
91
 
* Revision 6.16  2001/06/07 20:17:34  kans
92
 
* in QUERY_OpenServiceQuery, pass service to ConnNetInfo_Create
93
 
*
94
 
* Revision 6.15  2001/06/07 20:07:41  kans
95
 
* added QUERY_OpenServiceQuery
96
 
*
97
 
* Revision 6.14  2001/04/25 15:14:27  lavr
98
 
* SConnNetInfo::timeout is now a pointer
99
 
*
100
 
* Revision 6.13  2001/02/25 21:42:27  kans
101
 
* changed several Uint4s to size_t due to new prototypes
102
 
*
103
 
* Revision 6.12  2001/02/21 22:02:04  lavr
104
 
* Changes for use new CONN interface
105
 
*
106
 
* Revision 6.11  2000/08/18 19:08:58  kans
107
 
* added QUERY_WaitForNextMacEvent, otherwise QuickDraw collides with mmdbapi
108
 
*
109
 
* Revision 6.10  2000/06/30 18:16:09  kans
110
 
* protect against reentrant calls if resultproc is GUI and processes timer - showed up on PC/Windows, not Mac or UNIX version of Sequin
111
 
*
112
 
* Revision 6.9  2000/06/30 12:46:10  kans
113
 
* added QUERY_CloseQueue
114
 
*
115
 
* Revision 6.8  2000/06/29 18:27:10  kans
116
 
* QUERY_OpenUrlQuery has new EMIME_Type type and EMIME_Encoding encoding parameters
117
 
*
118
 
* Revision 6.7  2000/06/13 12:58:14  kans
119
 
* added closeConn parameter to QUERY_AddToQueue
120
 
*
121
 
* Revision 6.6  1999/07/28 21:09:15  vakatov
122
 
* Multiple fixes in QUERY_OpenUrlQuery() to make it work with a generic
123
 
* URL server;  also, pass arguments in the cmd.-line
124
 
*
125
 
* ==========================================================================
126
 
*/
 
1
/* $Id: urlquery.c,v 6.39 2006/04/19 02:11:23 lavr Exp $
 
2
 * ===========================================================================
 
3
 *
 
4
 *                            PUBLIC DOMAIN NOTICE
 
5
 *            National Center for Biotechnology Information (NCBI)
 
6
 *
 
7
 *  This software/database is a "United States Government Work" under the
 
8
 *  terms of the United States Copyright Act.  It was written as part of
 
9
 *  the author's official duties as a United States Government employee and
 
10
 *  thus cannot be copyrighted.  This software/database is freely available
 
11
 *  to the public for use. The National Library of Medicine and the U.S.
 
12
 *  Government do not place any restriction on its use or reproduction.
 
13
 *  We would, however, appreciate having the NCBI and the author cited in
 
14
 *  any work or product based on this material
 
15
 *
 
16
 *  Although all reasonable efforts have been taken to ensure the accuracy
 
17
 *  and reliability of the software and data, the NLM and the U.S.
 
18
 *  Government do not and cannot warrant the performance or results that
 
19
 *  may be obtained by using this software or data. The NLM and the U.S.
 
20
 *  Government disclaim all warranties, express or implied, including
 
21
 *  warranties of performance, merchantability or fitness for any particular
 
22
 *  purpose.
 
23
 *
 
24
 * ===========================================================================
 
25
 *
 
26
 * File Name:  urlquery.c
 
27
 *
 
28
 * Author:  Jonathan Kans
 
29
 *
 
30
 * Version Creation Date:   4/16/98
 
31
 *
 
32
 * $Revision: 6.39 $
 
33
 *
 
34
 * File Description: 
 
35
 *
 
36
 * Modifications:  
 
37
 * --------------------------------------------------------------------------
 
38
 * $Log: urlquery.c,v $
 
39
 * Revision 6.39  2006/04/19 02:11:23  lavr
 
40
 * QUERY_OpenServiceQuery: Use PostOverrideArg instead of AppendArg
 
41
 *
 
42
 * Revision 6.38  2006/04/19 01:41:52  lavr
 
43
 * QUERY_OpenServiceQuery(): Take advantage of ConnNetInfo_*Arg API
 
44
 *
 
45
 * Revision 6.37  2006/04/17 16:47:50  lavr
 
46
 * QUERY_OpenServiceQueryEx not to override but append argument in net_info
 
47
 *
 
48
 * Revision 6.36  2006/04/15 01:59:01  lavr
 
49
 * +QUERY_OpenServiceQueryEx
 
50
 *
 
51
 * Revision 6.35  2006/01/19 21:11:15  lavr
 
52
 * QUERY_SendQuery() to return EIO_Status
 
53
 *
 
54
 * Revision 6.34  2006/01/17 21:11:51  lavr
 
55
 * Formatting; a few minor code improvements; a few minor bugfixes
 
56
 *
 
57
 * Revision 6.33  2005/12/01 18:47:49  lavr
 
58
 * Code formatting
 
59
 *
 
60
 * Revision 6.32  2004/09/16 19:16:45  lavr
 
61
 * QUERY_OpenUrlQuery() to set port only if provided as non-zero
 
62
 *
 
63
 * Revision 6.31  2004/09/14 19:09:41  lavr
 
64
 * Correct last change log entry
 
65
 *
 
66
 * Revision 6.30  2004/09/14 18:52:12  kans
 
67
 * QUERY_OpenUrlQuery makes content type only if type < eMIME_T_Unknown
 
68
 *
 
69
 * Revision 6.29  2004/09/14 18:47:51  kans
 
70
 * QUERY_OpenUrlQuery looks for eMIME_T_Unknown, does not compost content type
 
71
 *
 
72
 * Revision 6.28  2004/02/23 15:30:02  lavr
 
73
 * New (last) parameter "how" added in CONN_Write() API call
 
74
 *
 
75
 * Revision 6.27  2003/10/21 18:27:43  lavr
 
76
 * QUERY_OpenServiceQuery(): Timeout override changed
 
77
 *
 
78
 * Revision 6.26  2003/09/03 21:15:29  lavr
 
79
 * Reuse "arguments" in QUERY_OpenServiceQuery() to be real service argument
 
80
 * (formely it was to modify the dispatcher and was not really used anywhere)
 
81
 *
 
82
 * Revision 6.25  2003/05/29 21:54:20  kans
 
83
 * QUERY_CheckQueue calls callback and dequeues connection if status is
 
84
 * eIO_Closed as well as eIO_Success  - callbacks already check status
 
85
 * before trying to read
 
86
 *
 
87
 * Revision 6.24  2003/05/29 19:02:56  kans
 
88
 * badstatus only used for future debugging purposes, does not block further
 
89
 * checks, also increments count of remaining queued connections for return
 
90
 * value
 
91
 *
 
92
 * Revision 6.23  2003/05/29 18:39:54  kans
 
93
 * QUERY_CheckQueue sets new badstatus field, protects against further checks,
 
94
 * if eIO_Timeout or eIO_Closed
 
95
 *
 
96
 * Revision 6.22  2002/11/21 20:35:41  kans
 
97
 * forgot to call ConnNetInfo_Destroy if bailing due to NULL connector
 
98
 *
 
99
 * Revision 6.21  2002/11/21 19:46:32  kans
 
100
 * if connector is NULL, do not call CONN_Create
 
101
 *
 
102
 * Revision 6.20  2002/08/07 18:45:17  lavr
 
103
 * Change from deprecated to current EIO_ReadMethod enums
 
104
 *
 
105
 * Revision 6.19  2002/07/02 17:17:58  kans
 
106
 * fixed error message
 
107
 *
 
108
 * Revision 6.18  2001/09/25 13:20:56  lavr
 
109
 * SERVICE_CreateConnectorEx() - number of args adjusted
 
110
 *
 
111
 * Revision 6.17  2001/08/16 18:07:04  kans
 
112
 * protect against calling CONN_Read or CONN_Write with NULL conn parameter
 
113
 *
 
114
 * Revision 6.16  2001/06/07 20:17:34  kans
 
115
 * in QUERY_OpenServiceQuery, pass service to ConnNetInfo_Create
 
116
 *
 
117
 * Revision 6.15  2001/06/07 20:07:41  kans
 
118
 * added QUERY_OpenServiceQuery
 
119
 *
 
120
 * Revision 6.14  2001/04/25 15:14:27  lavr
 
121
 * SConnNetInfo::timeout is now a pointer
 
122
 *
 
123
 * Revision 6.13  2001/02/25 21:42:27  kans
 
124
 * changed several Uint4s to size_t due to new prototypes
 
125
 *
 
126
 * Revision 6.12  2001/02/21 22:02:04  lavr
 
127
 * Changes for use new CONN interface
 
128
 *
 
129
 * Revision 6.11  2000/08/18 19:08:58  kans
 
130
 * added QUERY_WaitForNextMacEvent, otherwise QuickDraw collides with mmdbapi
 
131
 *
 
132
 * Revision 6.10  2000/06/30 18:16:09  kans
 
133
 * protect against reentrant calls if resultproc is GUI and processes timer -
 
134
 * showed up on PC/Windows, not Mac or UNIX version of Sequin
 
135
 *
 
136
 * Revision 6.9  2000/06/30 12:46:10  kans
 
137
 * added QUERY_CloseQueue
 
138
 *
 
139
 * Revision 6.8  2000/06/29 18:27:10  kans
 
140
 * QUERY_OpenUrlQuery has new EMIME_Type type and EMIME_Encoding
 
141
 * encoding parameters
 
142
 *
 
143
 * Revision 6.7  2000/06/13 12:58:14  kans
 
144
 * added closeConn parameter to QUERY_AddToQueue
 
145
 *
 
146
 * Revision 6.6  1999/07/28 21:09:15  vakatov
 
147
 * Multiple fixes in QUERY_OpenUrlQuery() to make it work with a generic
 
148
 * URL server;  also, pass arguments in the cmd.-line
 
149
 *
 
150
 * ==========================================================================
 
151
 */
127
152
 
128
153
#include "asnbuild.h"
129
154
#include <urlquery.h>
130
155
 
 
156
 
131
157
#ifdef OS_MAC
132
158
#include <Events.h>
133
159
 
134
160
NLM_EXTERN void QUERY_WaitForNextMacEvent (void)
135
 
 
136
161
{
137
162
  EventRecord  currEvent;
138
163
 
140
165
}
141
166
#endif
142
167
 
 
168
 
143
169
NLM_EXTERN CONN QUERY_OpenUrlQuery (
144
170
  Nlm_CharPtr host_machine,
145
171
  Nlm_Uint2 host_port,
150
176
  EMIME_Type type,
151
177
  EMIME_SubType subtype,
152
178
  EMIME_Encoding encoding,
153
 
  THCC_Flags flags)
 
179
  THCC_Flags flags
 
180
)
154
181
{
155
 
  CONN            conn = 0;
156
 
  CONNECTOR       connector;
157
 
  Nlm_Char        contentType[MAX_CONTENT_TYPE_LEN];
158
 
  SConnNetInfo*   info;
159
 
  Nlm_Char        path [PATH_MAX];
160
 
  EIO_Status      status;
161
 
  Nlm_CharPtr     userAgentName = NULL;
162
 
  Nlm_Char        user_header[sizeof(contentType) + 256];
 
182
  CONN           conn;
 
183
  CONNECTOR      connector;
 
184
  Nlm_Char       contentType [MAX_CONTENT_TYPE_LEN];
 
185
  SConnNetInfo*  info;
 
186
  Nlm_Char       path [PATH_MAX];
 
187
  EIO_Status     status;
 
188
  Nlm_CharPtr    userAgentName = NULL;
 
189
  Nlm_Char       user_header [sizeof(contentType) + 256];
163
190
 
164
 
  if (StringHasNoText (host_machine) || StringHasNoText (host_path))
 
191
  if (StringHasNoText (host_path))
165
192
    return NULL;
166
193
 
167
194
  /* allow the user to specify a prog. name, otherwise get from ProgramPath */
178
205
    userAgentName = "?";
179
206
  }
180
207
 
181
 
  /* was set content type from parameters - now explicit parameter */
182
 
  /*
183
 
  if ((flags & fHCC_UrlEncodeOutput) != 0) {
184
 
    encoding = eENCOD_Url;
185
 
  }
186
 
  */
187
 
 
188
208
  contentType [0] = '\0';
189
209
  if (type < eMIME_T_Unknown) {
190
 
    VERIFY( MIME_ComposeContentTypeEx(type, subtype, encoding,
191
 
                                      contentType, sizeof(contentType)) );
 
210
    VERIFY( MIME_ComposeContentTypeEx (type, subtype, encoding,
 
211
                                       contentType, sizeof(contentType)) );
192
212
  }
193
213
 
194
214
  /* set HTML header with program name as user agent */
196
216
 
197
217
  /* fill in connection info fields and create the connection */
198
218
  info = ConnNetInfo_Create(0);
199
 
  ASSERT( info );
 
219
  ASSERT ( info );
200
220
 
201
 
  if ( host_machine ) {
202
 
    StringNCpy_0(info->host, host_machine, sizeof(info->host));
 
221
  if (! StringHasNoText (host_machine)) {
 
222
    StringNCpy_0 (info->host, host_machine, sizeof(info->host));
203
223
  }
204
224
  if ( host_port ) {
205
225
    info->port = host_port;
206
226
  }
207
 
  if ( !StringHasNoText(arguments) ) {
208
 
    StringNCpy_0(info->args, arguments, sizeof(info->args));
 
227
  if ( !StringHasNoText (arguments)) {
 
228
    StringNCpy_0 (info->args, arguments, sizeof(info->args));
209
229
  }
210
 
  StringNCpy_0(info->path, host_path, sizeof(info->path));
 
230
  StringNCpy_0 (info->path, host_path, sizeof(info->path));
211
231
 
212
232
  if ( info->timeout ) {
213
233
    info->timeout->sec  = timeoutsec;
214
234
    info->timeout->usec = 0;
215
235
  }
216
236
 
217
 
  connector = HTTP_CreateConnector(info, user_header, flags);
 
237
  connector = HTTP_CreateConnector (info, user_header, flags);
 
238
  ConnNetInfo_Destroy (info);
 
239
 
218
240
  if (connector == NULL) {
219
241
    ErrPostEx (SEV_ERROR, 0, 0, "QUERY_OpenUrlQuery failed in HTTP_CreateConnector");
220
 
    ConnNetInfo_Destroy(info);
221
 
    return NULL;
222
 
  }
223
 
  status = CONN_Create(connector, &conn);
224
 
  if (status != eIO_Success) {
 
242
    conn = NULL;
 
243
  } else if ((status = CONN_Create(connector, &conn)) != eIO_Success) {
225
244
    ErrPostEx(SEV_ERROR, 0, 0, "QUERY_OpenUrlQuery failed in CONN_Create");
 
245
    ASSERT (conn == NULL);
226
246
  }
227
247
 
228
 
  /* cleanup & return */
229
 
  ConnNetInfo_Destroy(info);
230
248
  return conn;
231
249
}
232
250
 
233
251
 
234
 
NLM_EXTERN CONN QUERY_OpenServiceQuery (
 
252
NLM_EXTERN CONN QUERY_OpenServiceQueryEx (
235
253
  Nlm_CharPtr service,
236
 
  Nlm_CharPtr arguments,
237
 
  Nlm_Uint4   timeoutsec
 
254
  Nlm_CharPtr parameters,
 
255
  Nlm_Uint4   timeoutsec,
 
256
  Nlm_CharPtr arguments
238
257
)
239
 
 
240
258
{
241
 
  CONN            conn = 0;
242
 
  CONNECTOR       connector;
243
 
  size_t          n_written;
244
 
  size_t          args_len;
245
 
  SConnNetInfo*   info;
246
 
  EIO_Status      status;
 
259
  CONN           conn;
 
260
  CONNECTOR      connector;
 
261
  size_t         n_written;
 
262
  SConnNetInfo*  net_info;
 
263
  EIO_Status     status;
247
264
 
248
265
  /* fill in connection info fields and create the connection */
249
 
  info = ConnNetInfo_Create (service);
250
 
  ASSERT( info );
 
266
  net_info = ConnNetInfo_Create (service);
 
267
  ASSERT ( net_info );
251
268
 
252
269
  if (timeoutsec == (Nlm_Uint4)(-1)) {
253
 
    info->timeout = 0;
 
270
    net_info->timeout  = 0;
254
271
  } else if ( timeoutsec ) {
255
 
    info->tmo.sec  = timeoutsec;
256
 
    info->tmo.usec = 0;
257
 
    info->timeout  = &info->tmo;
 
272
    net_info->tmo.sec  = timeoutsec;
 
273
    net_info->tmo.usec = 0;
 
274
    net_info->timeout  = &net_info->tmo;
258
275
  }
259
 
 
260
 
  connector = SERVICE_CreateConnectorEx (service, fSERV_Any, info, 0);
 
276
  ConnNetInfo_PostOverrideArg(net_info, arguments, 0);
 
277
 
 
278
  connector = SERVICE_CreateConnectorEx (service, fSERV_Any, net_info, 0);
 
279
 
 
280
  ConnNetInfo_Destroy (net_info);
 
281
 
261
282
  if (connector == NULL) {
262
283
    ErrPostEx (SEV_ERROR, 0, 0, "QUERY_OpenServiceQuery failed in SERVICE_CreateConnectorEx");
263
 
    ConnNetInfo_Destroy (info);
264
 
    return NULL;
265
 
  }
266
 
 
267
 
  status = CONN_Create (connector, &conn);
268
 
  if (status != eIO_Success) {
 
284
    conn = NULL;
 
285
  } else if ((status = CONN_Create (connector, &conn)) != eIO_Success) {
269
286
    ErrPostEx (SEV_ERROR, 0, 0, "QUERY_OpenServiceQuery failed in CONN_Create");
270
 
    ASSERT( !conn );
271
 
  } else if ( !StringHasNoText (arguments) ) {
272
 
    args_len = StringLen (arguments);
273
 
    status = CONN_Write (conn, arguments, args_len,
274
 
                         &n_written, eIO_WritePersist);
 
287
    ASSERT (conn == NULL);
 
288
  } else if (! StringHasNoText (parameters)) {
 
289
    status = CONN_Write (conn, parameters, StringLen (parameters), &n_written, eIO_WritePersist);
275
290
    if (status != eIO_Success) {
276
 
      ErrPostEx (SEV_ERROR, 0, 0, "QUERY_OpenServiceQuery failed to write arguments in CONN_Write");
 
291
      ErrPostEx (SEV_ERROR, 0, 0, "QUERY_OpenServiceQuery failed to write service parameters in CONN_Write");
277
292
      CONN_Close (conn);
278
 
      conn = 0;
 
293
      conn = NULL;
279
294
    }
280
295
  }
281
296
 
282
 
  /* cleanup & return */
283
 
  ConnNetInfo_Destroy (info);
284
297
  return conn;
285
298
}
286
299
 
287
300
 
288
 
NLM_EXTERN void QUERY_SendQuery (
 
301
NLM_EXTERN CONN QUERY_OpenServiceQuery (
 
302
  Nlm_CharPtr service,
 
303
  Nlm_CharPtr parameters,
 
304
  Nlm_Uint4   timeoutsec
 
305
)
 
306
{
 
307
  return QUERY_OpenServiceQueryEx (service, parameters, timeoutsec, 0);
 
308
}
 
309
 
 
310
 
 
311
NLM_EXTERN EIO_Status QUERY_SendQuery (
289
312
  CONN conn
290
313
)
291
314
 
292
315
{
293
 
  STimeout  timeout;
 
316
  static const STimeout timeout = { 0 };
 
317
  EIO_Status            status;
294
318
 
295
 
  if (conn == 0) return;
 
319
  if (conn == NULL) return eIO_Closed;
296
320
 
297
321
  /* flush buffer, sending query, without waiting for response */
298
 
 
299
 
  timeout.sec  = 0;
300
 
  timeout.usec = 0;
301
 
  CONN_Wait (conn, eIO_Read, &timeout);
 
322
  status = CONN_Wait (conn, eIO_Read, &timeout);
 
323
  return status == eIO_Timeout ? eIO_Success : status;
302
324
}
303
325
 
 
326
 
304
327
#define URL_QUERY_BUFLEN  4096
305
328
 
306
329
NLM_EXTERN void QUERY_CopyFileToQuery (
307
330
  CONN conn,
308
331
  FILE *fp
309
332
)
310
 
 
311
333
{
312
334
  Nlm_CharPtr  buffer;
313
335
  size_t       ct;
321
343
    while ((ct = FileRead (buffer, 1, URL_QUERY_BUFLEN, fp)) > 0) {
322
344
      status = CONN_Write (conn, (const void *) buffer, ct,
323
345
                           &n_written, eIO_WritePersist);
 
346
      if (status != eIO_Success)
 
347
        break;
324
348
    }
 
349
    MemFree (buffer);
325
350
  }
326
 
  MemFree (buffer);
327
351
}
328
352
 
 
353
 
329
354
NLM_EXTERN void QUERY_CopyResultsToFile (
330
355
  CONN conn,
331
356
  FILE *fp
332
357
)
333
 
 
334
358
{
335
359
  Nlm_CharPtr  buffer;
336
360
  size_t       n_read;
340
364
 
341
365
  buffer = (Nlm_CharPtr) MemNew(URL_QUERY_BUFLEN + 1);
342
366
  if (buffer != NULL) {
343
 
    while ((status = CONN_Read (conn, buffer, URL_QUERY_BUFLEN, &n_read,
344
 
                                eIO_ReadPlain)) == eIO_Success) {
345
 
      FileWrite (buffer, 1, n_read, fp);
346
 
    }
 
367
    do {
 
368
      status = CONN_Read (conn, buffer, URL_QUERY_BUFLEN, &n_read, eIO_ReadPlain);
 
369
      if ( n_read )
 
370
        FileWrite (buffer, 1, n_read, fp);
 
371
    } while (status == eIO_Success);
 
372
    MemFree (buffer);
347
373
  }
348
 
  MemFree (buffer);
349
374
}
350
375
 
 
376
 
351
377
static Nlm_Int2 LIBCALL AsnIoConnWrite (
352
378
  Pointer ptr,
353
379
  Nlm_CharPtr buf,
354
380
  Nlm_Uint2 count
355
381
)
356
 
 
357
382
{
358
383
  size_t        bytes;
359
384
  AsnIoConnPtr  aicp;
360
385
 
361
386
  aicp = (AsnIoConnPtr) ptr;
362
387
  if (aicp == NULL || aicp->conn == NULL) return 0;
363
 
  CONN_Write (aicp->conn, (const void *) buf, (size_t) count,
364
 
              &bytes, eIO_WritePersist);
 
388
  CONN_Write (aicp->conn, buf, (size_t) count, &bytes, eIO_WritePersist);
365
389
  return (Nlm_Int2) bytes;
366
390
}
367
391
 
 
392
 
368
393
static Nlm_Int2 LIBCALL AsnIoConnRead (
369
394
  Pointer ptr,
370
395
  CharPtr buf,
371
396
  Nlm_Uint2 count
372
397
)
373
 
 
374
398
{
375
399
  size_t        bytes;
376
400
  AsnIoConnPtr  aicp;
377
401
 
378
402
  aicp = (AsnIoConnPtr) ptr;
379
403
  if (aicp == NULL || aicp->conn == NULL) return 0;
380
 
  CONN_Read (aicp->conn, (Pointer) buf, (Int4) count, &bytes, eIO_ReadPlain);
 
404
  CONN_Read (aicp->conn, buf, (size_t) count, &bytes, eIO_ReadPlain);
381
405
  return (Nlm_Int2) bytes;
382
406
}
383
407
 
 
408
 
384
409
NLM_EXTERN AsnIoConnPtr QUERY_AsnIoConnOpen (
385
410
  Nlm_CharPtr mode,
386
411
  CONN conn
387
412
)
388
 
 
389
413
{
390
414
  Int1          type;
391
415
  AsnIoConnPtr  aicp;
398
422
    type = (ASNIO_OUT | ASNIO_TEXT);
399
423
  else if (! StringCmp(mode, "wb"))
400
424
    type = (ASNIO_OUT | ASNIO_BIN);
401
 
  else
402
 
  {
 
425
  else {
403
426
    AsnIoErrorMsg (NULL, 81, mode);
404
427
    return NULL;
405
428
  }
410
433
  return aicp;
411
434
}
412
435
 
 
436
 
413
437
NLM_EXTERN AsnIoConnPtr QUERY_AsnIoConnClose (
414
438
  AsnIoConnPtr aicp
415
439
)
416
 
 
417
440
{
418
441
  if (aicp == NULL) return NULL;
419
442
  AsnIoClose (aicp->aip);
 
443
  aicp->conn = NULL;
420
444
  return (AsnIoConnPtr) MemFree (aicp);
421
445
}
422
446
 
 
447
 
423
448
typedef struct SQueueTag {
424
449
  CONN                conn;
425
450
  QueryResultProc     resultproc;
429
454
  struct SQueueTag*   next;
430
455
} SConnQueue, PNTR QueuePtr;
431
456
 
 
457
 
432
458
NLM_EXTERN void QUERY_AddToQueue (
433
459
  QUEUE* queue,
434
460
  CONN conn,
436
462
  Nlm_VoidPtr userdata,
437
463
  Nlm_Boolean closeConn
438
464
)
439
 
 
440
465
{
441
466
  QueuePtr       cqp;
442
467
  QueuePtr PNTR  qptr;
473
498
  }
474
499
}
475
500
 
 
501
 
476
502
static void QUERY_RemoveFromQueue (
477
503
  QUEUE* queue,
478
504
  CONN conn
479
505
)
480
 
 
481
506
{
482
507
  QueuePtr       curr;
483
508
  QueuePtr       next;
493
518
  while (curr != NULL) {
494
519
    next = curr->next;
495
520
    if (curr->conn == conn) {
496
 
      *(prev) = next;
 
521
      *prev = next;
497
522
      curr->next = NULL;
498
523
      MemFree (curr);
499
524
    } else {
503
528
  }
504
529
}
505
530
 
 
531
 
506
532
NLM_EXTERN Nlm_Int4 QUERY_CheckQueue (
507
533
  QUEUE* queue
508
534
)
509
 
 
510
535
{
511
536
  Nlm_Int4       count = 0;
512
537
  QueuePtr       curr;
529
554
      status = CONN_Wait (curr->conn, eIO_Read, &timeout);
530
555
 
531
556
      if (status == eIO_Success || status == eIO_Closed) {
532
 
 
533
557
        /* protect against reentrant calls if resultproc is GUI and processes timer */
534
558
        curr->protect = TRUE;
535
559
        if (curr->resultproc != NULL) {
540
564
          CONN_Close (curr->conn);
541
565
        }
542
566
        QUERY_RemoveFromQueue (queue, curr->conn);
543
 
 
544
567
      } else {
545
568
        count++;
546
569
      }
552
575
  return count;
553
576
}
554
577
 
 
578
 
555
579
NLM_EXTERN void QUERY_CloseQueue (
556
580
  QUEUE* queue
557
581
)
558
 
 
559
582
{
560
583
  QueuePtr       curr;
561
584
  QueuePtr       next;
577
600
    curr = next;
578
601
  }
579
602
}
580