Skip to content

Commit 93a35fc

Browse files
committed
Start work on OAuth for Windows.
1 parent 2bbdf97 commit 93a35fc

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

cups/oauth.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
#include "cups-private.h"
1212
#include "oauth.h"
1313
#include "form.h"
14-
#include <poll.h>
1514
#include <sys/stat.h>
16-
#ifdef __APPLE__
17-
# include <CoreFoundation/CoreFoundation.h>
18-
# include <CoreServices/CoreServices.h>
15+
#ifdef _WIN32
16+
# include <process.h>
1917
#else
20-
# include <spawn.h>
21-
# include <sys/wait.h>
18+
# include <poll.h>
19+
# ifdef __APPLE__
20+
# include <CoreFoundation/CoreFoundation.h>
21+
# include <CoreServices/CoreServices.h>
22+
# else
23+
# include <spawn.h>
24+
# include <sys/wait.h>
2225
extern char **environ; // @private@
23-
#endif // __APPLE__
26+
# endif // __APPLE__
27+
#endif // _WIN32
2428

2529

2630
//
@@ -329,7 +333,9 @@ cupsOAuthGetAuthorizationCode(
329333
size_t resourcelen; // Length of resource path
330334
http_addr_t addr; // Loopback listen address
331335
int port; // Port number
332-
struct pollfd polldata; // Poll data
336+
int fd = -1; // Listen file descriptor
337+
fd_set input; // Input file descriptors for select()
338+
struct timeval timeout; // Timeout for select()
333339
time_t endtime; // End time
334340
http_t *http; // HTTP client
335341
char *auth_code = NULL; // Authorization code
@@ -372,7 +378,7 @@ cupsOAuthGetAuthorizationCode(
372378
if (!strcmp(host, "localhost") || !strcmp(host, "127.0.0.1"))
373379
addr.ipv4.sin_addr.s_addr = htonl(0x7f000001);
374380

375-
polldata.fd = httpAddrListen(&addr, port);
381+
fd = httpAddrListen(&addr, port);
376382

377383
cupsConcatString(resource, "?", sizeof(resource));
378384
}
@@ -388,7 +394,7 @@ cupsOAuthGetAuthorizationCode(
388394

389395
for (port = 10000; port < 11000; port ++)
390396
{
391-
if ((polldata.fd = httpAddrListen(&addr, port)) >= 0)
397+
if ((fd = httpAddrListen(&addr, port)) >= 0)
392398
break;
393399
}
394400

@@ -398,13 +404,12 @@ cupsOAuthGetAuthorizationCode(
398404
redirect_uri = final_uri;
399405
}
400406

401-
DEBUG_printf("1cupsOAuthGetAuthorizationCode: Listen socket for port %d is %d (%s)", port, polldata.fd, strerror(errno));
407+
DEBUG_printf("1cupsOAuthGetAuthorizationCode: Listen socket for port %d is %d (%s)", port, fd, strerror(errno));
402408

403-
if (polldata.fd < 0)
409+
if (fd < 0)
404410
goto done;
405411

406-
resourcelen = strlen(resource);
407-
polldata.events = POLLIN | POLLERR | POLLHUP;
412+
resourcelen = strlen(resource);
408413

409414
// Point redirection to the local port...
410415
oauth_save_value(auth_uri, resource_uri, _CUPS_OTYPE_REDIRECT_URI, redirect_uri);
@@ -489,6 +494,10 @@ cupsOAuthGetAuthorizationCode(
489494
if (error != noErr)
490495
goto done;
491496

497+
#elif defined(_WIN32)
498+
if (_spawnl(_P_WAIT, "start", "", url, NULL))
499+
goto done;
500+
492501
#else
493502
pid_t pid = 0; // Process ID
494503
int estatus; // Exit status
@@ -511,10 +520,16 @@ cupsOAuthGetAuthorizationCode(
511520

512521
while (auth_code == NULL && time(NULL) < endtime)
513522
{
514-
if (poll(&polldata, 1, 1000) == 1 && (polldata.revents & POLLIN))
523+
timeout.tv_sec = 1;
524+
timeout.tv_usec = 0;
525+
526+
FD_ZERO(&input);
527+
FD_SET(fd, &input);
528+
529+
if (select(fd + 1, &input, /*writefds*/NULL, /*errorfds*/NULL, &timeout) > 0 && FD_ISSET(fd, &input))
515530
{
516531
// Try accepting a connection...
517-
if ((http = httpAcceptConnection(polldata.fd, true)) != NULL)
532+
if ((http = httpAcceptConnection(fd, true)) != NULL)
518533
{
519534
// Respond to HTTP requests...
520535
while (auth_code == NULL && time(NULL) < endtime && httpWait(http, 1000))
@@ -655,8 +670,8 @@ cupsOAuthGetAuthorizationCode(
655670
done:
656671

657672
// Free strings, close the listen socket, and return...
658-
if (polldata.fd >= 0)
659-
httpAddrClose(&addr, polldata.fd);
673+
if (fd >= 0)
674+
httpAddrClose(&addr, fd);
660675

661676
free(client_id);
662677
free(code_verifier);

0 commit comments

Comments
 (0)