Ignore:
Timestamp:
2001-04-05T17:08:52+12:00 (23 years ago)
Author:
sjboddie
Message:

Had a bit of a tidy up in the fnord webserver code. The main change of note
was the removal of our reliance on the MAX_URL_SIZE constant. URLs and post
data of any length should now work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/w32server/startbrowser.cpp

    r611 r2286  
     1/**********************************************************************
     2 *
     3 * startbrowser.cpp
     4 * Copyright (C) 1999  The New Zealand Digital Library Project
     5 *
     6 * A component of the Greenstone digital library software
     7 * from the New Zealand Digital Library Project at the
     8 * University of Waikato, New Zealand.
     9 *
     10 * This program is free software; you can redistribute it and/or modify
     11 * it under the terms of the GNU General Public License as published by
     12 * the Free Software Foundation; either version 2 of the License, or
     13 * (at your option) any later version.
     14 *
     15 * This program is distributed in the hope that it will be useful,
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 * GNU General Public License for more details.
     19 *
     20 * You should have received a copy of the GNU General Public License
     21 * along with this program; if not, write to the Free Software
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     23 *
     24 *********************************************************************/
     25
    126#include "startbrowser.h"
    227#include <windows.h>
     
    2146static HSZ hszActivateTopic = NULL;
    2247
    23 
    2448// budget callback
    2549HDDEDATA CALLBACK DdeCallback(UINT uType,     /* transaction type                 */
    26                               UINT uFmt,      /* clipboard data format            */
    27                               HCONV hconv,    /* handle of conversation           */
    28                               HSZ hsz1,       /* handle of string                 */
    29                               HSZ hsz2,       /* handle of string                 */
    30                               HDDEDATA hdata, /* handle of global memory object   */
    31                               DWORD dwData1,  /* transaction-specific data        */
    32                               DWORD dwData2)  /* transaction-specific data        */
     50                  UINT uFmt,      /* clipboard data format            */
     51                  HCONV hconv,    /* handle of conversation           */
     52                  HSZ hsz1,       /* handle of string                 */
     53                  HSZ hsz2,       /* handle of string                 */
     54                  HDDEDATA hdata, /* handle of global memory object   */
     55                  DWORD dwData1,  /* transaction-specific data        */
     56                  DWORD dwData2)  /* transaction-specific data        */
    3357{
    34     return (HDDEDATA) NULL;
    35 }
    36 
     58  return (HDDEDATA) NULL;
     59}
    3760
    3861// returns 0 on failure
    3962static int talkdde (HSZ hszServName, HSZ hszSysTopic, char *requeststr) {
    40     HSZ hszRequest;
    41     HCONV hConv;
    42     HDDEDATA result;
    43 
    44     // try to connect to the newly started browser
    45     hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
    46     if (hConv == NULL) return 0;
    47 
    48     hszRequest = DdeCreateStringHandle (idInst, requeststr, CP_WINANSI);
    49 
    50 //HDDEDATA hRetVal = DdeClientTransaction(NULL, 0ul, m_hConv,
    51 //                              hszItem, CF_TEXT,
    52 //                              XTYP_REQUEST,
    53 //                              DDETIMEOUT, NULL);
    54     result = DdeClientTransaction (
    55         NULL,           // pointer to data to pass to server
    56         0,              // length of data
    57         hConv,          // handle to conversation
    58         hszRequest,     // handle to item name string
    59         CF_TEXT,        // clipboard data format
    60         XTYP_REQUEST,   // transaction type
    61         SB_DDETIMEOUT,  // time-out duration
    62         NULL            // pointer to transaction result
    63     );
    64 
    65     if (result != NULL) DdeFreeDataHandle (result);
    66     DdeDisconnect(hConv);
    67     DdeFreeStringHandle (idInst, hszRequest);
    68 
    69     return 1;
    70 }
    71 
     63  HSZ hszRequest;
     64  HCONV hConv;
     65  HDDEDATA result;
     66 
     67  // try to connect to the newly started browser
     68  hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
     69  if (hConv == NULL) return 0;
     70 
     71  hszRequest = DdeCreateStringHandle (idInst, requeststr, CP_WINANSI);
     72  result = DdeClientTransaction (
     73                 NULL,          // pointer to data to pass to server
     74                 0,             // length of data
     75                 hConv,         // handle to conversation
     76                 hszRequest,    // handle to item name string
     77                 CF_TEXT,       // clipboard data format
     78                 XTYP_REQUEST,  // transaction type
     79                 SB_DDETIMEOUT, // time-out duration
     80                 NULL           // pointer to transaction result
     81                 );
     82
     83  if (result != NULL) DdeFreeDataHandle (result);
     84  DdeDisconnect(hConv);
     85  DdeFreeStringHandle (idInst, hszRequest);
     86
     87  return 1;
     88}
    7289
    7390// returns 0 on failure
    7491static int openurl(HSZ hszServName, HSZ hszActivateTopic, HSZ hszOpenURLTopic, char *url) {
    75     char requeststr[65600];
    76 
    77     // bring the browser to the front, don't worry if this fails
    78     talkdde (hszServName, hszActivateTopic, "0xFFFFFFFF,0x0");
    79 
    80     // tell the browser to open a url
    81     strcpy (requeststr, "\"");
    82     strcat (requeststr, url);
    83     strcat (requeststr, "\",,0xFFFFFFFF,0x0,,,");
    84 
    85     return talkdde (hszServName, hszOpenURLTopic, requeststr);
     92  char requeststr[65600];
     93 
     94  // bring the browser to the front, don't worry if this fails
     95  talkdde (hszServName, hszActivateTopic, "0xFFFFFFFF,0x0");
     96 
     97  // tell the browser to open a url
     98  strcpy (requeststr, "\"");
     99  strcat (requeststr, url);
     100  strcat (requeststr, "\",,0xFFFFFFFF,0x0,,,");
     101 
     102  return talkdde (hszServName, hszOpenURLTopic, requeststr);
    86103}
    87104
    88105// returns 0 on failure
    89106static int tryconnect(HSZ hszServName, HSZ hszSysTopic) {
    90     // try to connect to a running browser
    91     HCONV hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
    92     if (hConv == NULL) return 0;
    93     DdeDisconnect(hConv);
    94     return 1;
     107  // try to connect to a running browser
     108  HCONV hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
     109  if (hConv == NULL) return 0;
     110  DdeDisconnect(hConv);
     111  return 1;
    95112}
    96113
    97114static void freestrings () {
    98     // free up any currently allocated strings
    99     if (hszNetscapeName != NULL) DdeFreeStringHandle (idInst, hszNetscapeName);
    100     hszNetscapeName = NULL;
    101     if (hszIExploreName != NULL) DdeFreeStringHandle (idInst, hszIExploreName);
    102     hszIExploreName = NULL;
    103     if (hszOpenURLTopic != NULL) DdeFreeStringHandle (idInst, hszOpenURLTopic);
    104     hszOpenURLTopic = NULL;
    105     if (hszActivateTopic != NULL) DdeFreeStringHandle (idInst, hszActivateTopic);
    106     hszActivateTopic = NULL;
    107 }
    108 
    109 
     115  // free up any currently allocated strings
     116  if (hszNetscapeName != NULL) DdeFreeStringHandle (idInst, hszNetscapeName);
     117  hszNetscapeName = NULL;
     118  if (hszIExploreName != NULL) DdeFreeStringHandle (idInst, hszIExploreName);
     119  hszIExploreName = NULL;
     120  if (hszOpenURLTopic != NULL) DdeFreeStringHandle (idInst, hszOpenURLTopic);
     121  hszOpenURLTopic = NULL;
     122  if (hszActivateTopic != NULL) DdeFreeStringHandle (idInst, hszActivateTopic);
     123  hszActivateTopic = NULL;
     124}
    110125
    111126static void getbrowserinfo() {
    112     freestrings();
    113 
    114     hszNetscapeName = DdeCreateStringHandle(idInst,"NETSCAPE",CP_WINANSI);
    115     hszIExploreName = DdeCreateStringHandle(idInst,"IExplore",CP_WINANSI);
    116 
    117     hszOpenURLTopic = DdeCreateStringHandle(idInst,"WWW_OpenURL",CP_WINANSI);
    118     hszActivateTopic = DdeCreateStringHandle(idInst,"WWW_Activate",CP_WINANSI);
    119 }
    120 
    121 
     127  freestrings();
     128 
     129  hszNetscapeName = DdeCreateStringHandle(idInst,"NETSCAPE",CP_WINANSI);
     130  hszIExploreName = DdeCreateStringHandle(idInst,"IExplore",CP_WINANSI);
     131 
     132  hszOpenURLTopic = DdeCreateStringHandle(idInst,"WWW_OpenURL",CP_WINANSI);
     133  hszActivateTopic = DdeCreateStringHandle(idInst,"WWW_Activate",CP_WINANSI);
     134}
    122135
    123136// returns SB_NOERROR on success,
    124137// SB_ALREADYINIT or SB_DDE_FAILINIT on failure
    125138int initstartbrowser () {
    126     if (inited) return SB_ALREADYINIT;
    127 
    128 // from nstest if(DdeInitialize(&CDDEObject::m_dwidInst, NstestDdeCallBack, APPCLASS_STANDARD, 0ul))
    129     if (DdeInitialize (&idInst, (PFNCALLBACK) DdeCallback,
    130         APPCLASS_STANDARD, 0)
    131 //      CBF_FAIL_EXECUTES | CBF_SKIP_ALLNOTIFICATIONS, 0)
    132             != DMLERR_NO_ERROR) {
    133         freestrings();
    134         inited = 0;
    135         return SB_DDE_FAILINIT;
    136     }
    137 
    138     getbrowserinfo();
    139    
    140     inited = 1;
    141     return SB_NOERROR;
     139  if (inited) return SB_ALREADYINIT;
     140 
     141  // from nstest if(DdeInitialize(&CDDEObject::m_dwidInst, NstestDdeCallBack, APPCLASS_STANDARD, 0ul))
     142  if (DdeInitialize (&idInst, (PFNCALLBACK) DdeCallback,
     143             APPCLASS_STANDARD, 0)
     144      != DMLERR_NO_ERROR) {
     145    freestrings();
     146    inited = 0;
     147    return SB_DDE_FAILINIT;
     148  }
     149
     150  getbrowserinfo();
     151 
     152  inited = 1;
     153  return SB_NOERROR;
    142154}
    143155
     
    146158// SB_DDE_FAILDEINIT of failure
    147159int deinitstartbrowser () {
    148     // make sure this has been inited
    149     // not being inited here, however, is not
    150     // important
    151     if (!inited) return SB_NOERROR;
    152     inited = 0;
    153 
    154     freestrings();
    155 
    156     if (DdeUninitialize(idInst)) return SB_NOERROR;
    157 
    158     return SB_DDE_FAILDEINIT;
     160  // make sure this has been inited
     161  // not being inited here, however, is not
     162  // important
     163  if (!inited) return SB_NOERROR;
     164  inited = 0;
     165 
     166  freestrings();
     167 
     168  if (DdeUninitialize(idInst)) return SB_NOERROR;
     169 
     170  return SB_DDE_FAILDEINIT;
    159171}
    160172
     
    166178// SB_FAILCONNECTBROWSER on failure
    167179int startbrowser (char *url, char *browser_exe, char *startdir) {
    168     char newcommand[SB_COMLEN];
    169     int usingnetscape = 0;
    170     int usingiexplore = 0;
    171 
    172     if (!inited) return SB_NOTINITED; // has to be inited first
    173 
    174     // find out which browser we are dealing with
    175     strcpy (newcommand, browser_exe);
    176     _strlwr (newcommand);
    177     if (strstr (newcommand, "netscape.exe") != NULL) {
    178         // netscape
    179         usingnetscape = 1;
    180     } else if (strstr (newcommand, "iexplore.exe") != NULL) {
    181         // internet explorer
    182         usingiexplore = 1;
    183     }
    184 
    185     // only try to communicate with a running browser if a startdir
    186     // is not specified
    187     if (startdir == NULL) {
    188         if (usingnetscape && hszNetscapeName != NULL &&
    189                     openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
    190             return SB_NOERROR;
    191         }
    192         if (usingiexplore && hszIExploreName != NULL &&
    193                     openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
    194             return SB_NOERROR;
    195         }
    196     }
    197 
    198     strcpy (newcommand, browser_exe);
    199 
    200     // don't put the url on the command line for internet explorer
    201     if (usingiexplore) {
    202         strcat (newcommand, " -nohome");
    203 
    204     } else {
    205         // will have to put the url on the command line
    206         strcat (newcommand, " \"");
    207         strcat (newcommand, url);
    208         strcat (newcommand, "\"");
    209     }
    210 
    211     // change the working directory (if needed)
    212     char cwd[1024];
    213     cwd[0] = '\0';
    214     if (startdir != NULL) {
    215         _getcwd(cwd, 1024);
    216         _chdir(startdir);
    217     }
    218 
    219     // attempt to start the browser
    220     int res = WinExec(newcommand, SW_SHOW);
    221 
    222     // change the working directory back (if needed)
    223     if (startdir != NULL) _chdir(cwd);
    224 
    225     if (res == ERROR_BAD_FORMAT) return SB_FAIL_BADFORMAT;
    226     if ((res == ERROR_FILE_NOT_FOUND) || (res == ERROR_PATH_NOT_FOUND))
    227         return SB_FAIL_NOTFOUND;
    228     if (res <= 31) return SB_FAIL_NORESOURCES;
    229 
    230     numbrowserstarted++;
    231 
    232     // if we aren't using iexplore then the url was
    233     // put on the command line and there is nothing left to do
    234     if (!usingiexplore) return SB_NOERROR;
    235 
    236     // connect to the browser and get it to open a page,
    237     // time out after 1 minute
    238     DWORD lastcheck = GetTickCount ();
    239     while (DiffTickCounts (lastcheck, GetTickCount()) <= 60000) {
    240         if (usingnetscape && hszNetscapeName != NULL &&
    241                     openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
    242             return SB_NOERROR;
    243         }
    244         if (usingiexplore && hszIExploreName != NULL &&
    245                     openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
    246             return SB_NOERROR;
    247         }
    248     }
    249 
    250     // timed out trying to connect to the browser
    251     return SB_FAILCONNECTBROWSER;
    252 }
    253 
    254 
     180  char newcommand[SB_COMLEN];
     181  int usingnetscape = 0;
     182  int usingiexplore = 0;
     183 
     184  if (!inited) return SB_NOTINITED; // has to be inited first
     185 
     186  // find out which browser we are dealing with
     187  strcpy (newcommand, browser_exe);
     188  _strlwr (newcommand);
     189  if (strstr (newcommand, "netscape.exe") != NULL) {
     190    // netscape
     191    usingnetscape = 1;
     192  } else if (strstr (newcommand, "iexplore.exe") != NULL) {
     193    // internet explorer
     194    usingiexplore = 1;
     195  }
     196 
     197  // only try to communicate with a running browser if a startdir
     198  // is not specified
     199  if (startdir == NULL) {
     200    if (usingnetscape && hszNetscapeName != NULL &&
     201    openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
     202      return SB_NOERROR;
     203    }
     204    if (usingiexplore && hszIExploreName != NULL &&
     205    openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
     206      return SB_NOERROR;
     207    }
     208  }
     209 
     210  strcpy (newcommand, browser_exe);
     211 
     212  // don't put the url on the command line for internet explorer
     213  if (usingiexplore) {
     214    strcat (newcommand, " -nohome");
     215   
     216  } else {
     217    // will have to put the url on the command line
     218    strcat (newcommand, " \"");
     219    strcat (newcommand, url);
     220    strcat (newcommand, "\"");
     221  }
     222 
     223  // change the working directory (if needed)
     224  char cwd[1024];
     225  cwd[0] = '\0';
     226  if (startdir != NULL) {
     227    _getcwd(cwd, 1024);
     228    _chdir(startdir);
     229  }
     230 
     231  // attempt to start the browser
     232  int res = WinExec(newcommand, SW_SHOW);
     233 
     234  // change the working directory back (if needed)
     235  if (startdir != NULL) _chdir(cwd);
     236 
     237  if (res == ERROR_BAD_FORMAT) return SB_FAIL_BADFORMAT;
     238  if ((res == ERROR_FILE_NOT_FOUND) || (res == ERROR_PATH_NOT_FOUND))
     239    return SB_FAIL_NOTFOUND;
     240  if (res <= 31) return SB_FAIL_NORESOURCES;
     241 
     242  numbrowserstarted++;
     243 
     244  // if we aren't using iexplore then the url was
     245  // put on the command line and there is nothing left to do
     246  if (!usingiexplore) return SB_NOERROR;
     247 
     248  // connect to the browser and get it to open a page,
     249  // time out after 1 minute
     250  DWORD lastcheck = GetTickCount ();
     251  while (DiffTickCounts (lastcheck, GetTickCount()) <= 60000) {
     252    if (usingnetscape && hszNetscapeName != NULL &&
     253    openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
     254      return SB_NOERROR;
     255    }
     256    if (usingiexplore && hszIExploreName != NULL &&
     257    openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
     258      return SB_NOERROR;
     259    }
     260  }
     261 
     262  // timed out trying to connect to the browser
     263  return SB_FAILCONNECTBROWSER;
     264}
    255265
    256266// returns SB_NOERROR on success,
    257267// SB_NOTINITED, SB_FAILCONNECTBROWSER on failure
    258268int browserrunning (char *browser_exe) {
    259     char newcommand[SB_COMLEN];
    260     if (!inited) return SB_NOTINITED; // has to be inited first
    261 
    262     strcpy (newcommand, browser_exe);
    263     _strlwr (newcommand);
    264 
    265     if ((strstr (newcommand, "netscape.exe") != NULL) && (hszNetscapeName != NULL) &&
    266             tryconnect(hszNetscapeName, hszOpenURLTopic)) {
    267         // succeeded connecting to netscape
    268         return SB_NOERROR;
    269 
    270     } else if ((strstr (newcommand, "iexplore.exe") != NULL) && (hszIExploreName != NULL) &&
    271             tryconnect(hszIExploreName, hszOpenURLTopic)) {
    272         // succeeded connecting to internet explorer
    273         return SB_NOERROR;
    274     }
    275 
    276     return SB_FAILCONNECTBROWSER;
    277 }
     269  char newcommand[SB_COMLEN];
     270  if (!inited) return SB_NOTINITED; // has to be inited first
     271 
     272  strcpy (newcommand, browser_exe);
     273  _strlwr (newcommand);
     274 
     275  if ((strstr (newcommand, "netscape.exe") != NULL) && (hszNetscapeName != NULL) &&
     276      tryconnect(hszNetscapeName, hszOpenURLTopic)) {
     277    // succeeded connecting to netscape
     278    return SB_NOERROR;
     279   
     280  } else if ((strstr (newcommand, "iexplore.exe") != NULL) && (hszIExploreName != NULL) &&
     281         tryconnect(hszIExploreName, hszOpenURLTopic)) {
     282    // succeeded connecting to internet explorer
     283    return SB_NOERROR;
     284  }
     285 
     286  return SB_FAILCONNECTBROWSER;
     287}
Note: See TracChangeset for help on using the changeset viewer.